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