Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
});
like image 854
Dano Avatar asked Jan 23 '26 11:01

Dano


1 Answers

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
});
like image 95
Benjamin Gruenbaum Avatar answered Jan 25 '26 23:01

Benjamin Gruenbaum