Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

co and yield* when a promise is rejected ? #284

Open
rehia opened this issue Jul 26, 2016 · 3 comments
Open

co and yield* when a promise is rejected ? #284

rehia opened this issue Jul 26, 2016 · 3 comments

Comments

@rehia
Copy link

rehia commented Jul 26, 2016

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:

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 :)

@rehia
Copy link
Author

rehia commented Aug 10, 2016

hi all, any idea about this ?
would love to share about this !

@targos
Copy link

targos commented Aug 12, 2016

You can only use yield* to delegate to other generators in co. The reason is that the throw call (made by co because of the rejected promise) is propagated to the array iterator and this iterator does not implement a throw method.

See https://bugs.chromium.org/p/v8/issues/detail?id=3484

The solution to your problem is just to use yield array

@v-limliu
Copy link

the same to me , how to reject a error with co? since only yield is used ,is there anyway to do this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants