Skip to content

Instantly share code, notes, and snippets.

@zlatkov
Created October 11, 2017 14:40
Show Gist options
  • Save zlatkov/e7098be769383bac5336c54d952beb53 to your computer and use it in GitHub Desktop.
Save zlatkov/e7098be769383bac5336c54d952beb53 to your computer and use it in GitHub Desktop.
function sum(xPromise, yPromise) {
// `Promise.all([ .. ])` takes an array of promises,
// and returns a new promise that waits on them
// all to finish
return Promise.all([xPromise, yPromise])
// when that promise is resolved, let's take the
// received `X` and `Y` values and add them together.
.then(function(values){
// `values` is an array of the messages from the
// previously resolved promises
return values[0] + values[1];
} );
}
// `fetchX()` and `fetchY()` return promises for
// their respective values, which may be ready
// *now* or *later*.
sum(fetchX(), fetchY())
// we get a promise back for the sum of those
// two numbers.
// now we chain-call `then(...)` to wait for the
// resolution of that returned promise.
.then(function(sum){
console.log(sum);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment