#284·co

co and yield* when a promise is rejected ?

Author: rehiaCreated Jul 26, 2016Updated Jun 14, 2017

I just faced an issue mixing generators and yield*, co, and node. It seemed to be the right place to start, but I'm not really sure this is a problem with co. Let me explain:

I usually use this code to make the node task fail whenever an error occurs in a generators chain handled by co, because of Promise specification, where errors are muted if not handled:

javascript
process.on('unhandledRejection', (reason, p) => {
  throw reason;
});

I also find yield* a cleaner way to yield the values returned by an array of promises, instead of Promise.all(). At first, I thought that it pretty did the same thing as Promise.all(), but when one of the promises is rejected, then I had the following error:

/somewhere/yield.js:14
  throw reason;
  ^
unhandled TypeError: Cannot read property 'done' of undefined
    at next (/somewhere/node_modules/co/index.js:98:14)
    at onRejected (/somewhere/node_modules/co/index.js:85:7)
    ....

Here is the full code: https://gist.github.com/rehia/6b13bd8202a37fbb58c82cbdd9df9b83

I use Node 6.3.1 and co 4.6.0. http://node.green/#generators tells that yield* is fully supported by Node... If you have any thoughts on this, I'll be glad to know :)