Dano
Dano

Reputation: 139

Promise that returns a promise?

I am trying to create the following promise syntaxes:

I want the secondary then to be called after the post function, how can I do that?

randomBytes.then(function() {
   return requestPromise.post(....);
}).then(function() {
   // I want this part to be called after the POST
});

Upvotes: 3

Views: 149

Answers (2)

Jie Zdok
Jie Zdok

Reputation: 131

it should work. and you can add a 'then' to get the post value;

randomBytes.then(function() {
   return requestPromise.post(....).then((value,...)=>{return value;});
}).then(function(value) {
   //console.log(value);
});

Upvotes: 0

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276276

The fact this already works (as the comment said - assuming .post returns a promise) is pretty much the point of then - it allows you to wait for stuff.

If you return a promise from then, the chain will assume its state and thus will only call the following segments after it has completed.

randomBytes.then(function() {
   return requestPromise.post(....);
}).then(function() {
   // WILL ALWAYS BE CALLED AFTER THE POST
});

Upvotes: 3

Related Questions