Sunday, 4 June 2017

Node.js Generator "already running" when throwing errors to itself

I am using the following pattern in my code:

let gen = (function*(){
    let foo = yield setTimout((err, something)=> err ? gen.throw(err) : gen.next(something), 1000, new Error('My error message'), null);
    console.log(something);
})();
gen.next();

Where setTimeout is some async function (e.g. database fetch or something). This works inside browser, but when I run it inside a mocha integration test for my Sailsjs app from node.js v 7.9.0, instead of displaying My error message, when the error occurs (e.g. when Sails returns an error on exec), it is displaying Uncaught TypeError: Generator is already running. Happy case (i.e. when I call gen.next(something)) works without problems. What gives?

And before trigger-happy flag people jump in: no, when I get syntax errors or other errors that are not gen.thrown, like in this question, the behaviour is correct - error message is appropriate and stack trace points to the right place.



via Megakoresh

No comments:

Post a Comment