In ES2017, we have async/await
for unwrapping promises. However, this is just syntastic sugar for the use of coroutines, which are already available in ES6 (see the recommended modules tj/co and Bluebird's Promise.coroutine()).
Below is a sample implementation of spawn
showing how it works. It takes a generator function, inside of which yield
can be used to unwrap promises for synchronous-style code. spawn
always returns a promise.
You can install this gist via npm to tinker with it yourself, or copy/paste the function into any environment that supports generators.
IMPORTANT: Use this code only for tinkering and education purposes, not for use in a real project. While you're free to do so, it's a MUCH beter practice to use an existing, published module for such functionality, such as the two linked above, or better yet use async/await via babel.
To install:
npm install gist:91ec6f6e65583a1ddacf
Sample usage (also using let
, const
, and for..of
):
const spawn = require('./spawn');
const result = spawn(function*() {
let actions = [Promise.resolve('one'), Promise.resolve('two'), Promise.reject(new Error('denied!'))];
let values = [];
for (let promise of actions) {
try {
let result = yield promise;
console.log("Got: ", result);
values.push(result);
} catch(e) {
console.error("Caught rejection:", e);
}
}
return values;
});
result.then(res => console.log("Final result: ", res)
.catch(err => console.error("Rejected: ", err);
// "Got: one"
// "Got: two"
// "Caught rejection: Denied!"
// "Final result: [one, two]"