Passing variables through a promise chain

Is there a better way to do this?

let foo; return functionA().then(result => { foo = result; return functionB(); }).then(bar => { return functionC(foo, bar); }); 

Note that the result of functionA must be entered in functionC . Using a variable outside the scope of the promise works fine, but it doesn't look good. Is there a clean idiomatic way to do this?

Please note that I have no way to change the API of any of the functions that I call.

+5
source share
2 answers

You can try using Promise.all() , which you can pass to the promises array, and it provides an array of answers in the then() callback when all the promises have passed, have been resolved. You can access these array values ​​to go to functionC :

 Promise.all([functionA, functionB]).then(values => functionC(values[0], values[1])); 

It might be a little cleaner (without nesting), since it doesn't look like the response from functionA should be passed to functionB .

Otherwise, the nesting will look like this:

 return functionA().then(foo => { return functionB().then(bar => { return functionC(foo, bar); }); }); 

Hope this helps.

+6
source

One option, as Alexander Promise.all(functionA(), functionB()) writes, use Promise.all(functionA(), functionB()) . This performs two functions at the same time. If this is what you want, you can use this answer. If, however, you want them to happen one after another, and then also be able to pass the result to another handler, you can do this:

 function functionA() { return new Promise(resolve => resolve(1)); } function functionB() { return new Promise(resolve => resolve(2)); } function functionC(first, second) { return first + second; } functionA() .then(result => Promise.all([result, functionB()])) .then(function([first, second]) { return functionC(first, second); }) .then(result => { console.log(result); }); 

Functions are obviously simplified, but the great thing about Promises is that it doesn't matter: we don't care how complicated they are or how long they take. Yay Promises!

The smartest thing is that Promise.all doesn't mind if the values ​​you pass are not Promises. If they represent any other value, they are considered a promise that is immediately resolved. (In the same way, you can do Promise.resolve(42).then(...) .) That way we can do Promise.all([result, functionB()]) . This says: "Give me a promise to be resolved when you get the final value for result and functionB() and pass both values ​​to." This happens directly in the case of result and at some unspecified time in the case of functionB .

The returned values ​​are then passed as an array of the next then function.

 .then(function([first, second]) { return functionC(first, second); }) 

Then it receives the values ​​as an array (see using destructuring in the parameter list) and passes these values ​​to functionC . Then we do the last function then to show the result.

+3
source

Source: https://habr.com/ru/post/1263476/


All Articles