Consider the following code which I took from https://stackoverflow.com/a/28250704/460084
function getExample() {
var a = promiseA(…);
var b = a.then(function(resultA) {
// some processing
return promiseB(…);
});
return Promise.all([a, b]).spread(function(resultA, resultB) {
// more processing
return // something using both resultA and resultB
});
}
and a demo of the code I created https://jsfiddle.net/Lsobypup/
The idea is to run multiple promises and return some composite value based on their results.
What I don't understand is why in the code above promiseA runs only once ? it seems to me that with Promise.all([a, b]) it should run first when a is evaluated and then again when b is evaluated as it depends on a. But as the demo shows this does not happen.
Is there some magic in Promise.all to make this happen ? and what are the rules around this kind of behaviour ?