3

I try make some node script with co. It works well, but a have big delay before script finished. (I got "Ok" or "Bad" after one second, but script finishes 7 seconds after it). What I missed?

co(function *() {
    let errors = yield someCheck(process.argv);
    if (!errors) {
        console.log('Ok');
    } else {
        console.log('Bad');
    }

})(function(e) {

    if (e) {
        console.log(e);
    }
});
Stepan Loginov
  • 1,497
  • 4
  • 17
  • 40

2 Answers2

0

I get a typeError when I run your code. I'm not sure what you are trying to do there, but I think you cannot pass the error handler as a second argument when calling co(), you have to use then() or catch() for error handling.

// Here's an example
co(function*() {
  var result = yield Promise.resolve(true);
  return result;
}).then(function(value) {
  console.log(value);
}, function(err) {
  console.error(err.stack);
});

// you can also catch the error
co(function *(){
  // yield any promise 
  var result = yield Promise.resolve(true);
}).catch(onerror);
function onerror(err) {
  // log any uncaught errors 
  // co will not throw any errors you do not handle!!! 
  // HANDLE ALL YOUR ERRORS!!! 
  console.error(err.stack);
}
Relu Mesaros
  • 4,252
  • 3
  • 21
  • 33
0

I think process.exit() would fix your issue.

yevhene
  • 175
  • 6