Skip to content Skip to sidebar Skip to footer

Reject In Promise Undefined

I tried below function use co and javascript promise test, the fulfill will success return but reject not, and catch error undefined. and the flow can't continue. why? Error: >

Solution 1:

catch error undefined

No. It catches the error 'error', the value you rejected with. Of course, it's not really an Error but a string, and as such it does not have a .stack property - that's why it logs undefined. Fix your code by doing

reject(newError('…'));

See also Should a Promise.reject message be wrapped in Error?

the flow can't continue. why?

Well, because you've got an error, and thrown exceptions do have this behaviour. You'll also want to send a response in your error handler!

co(function *() {
  …
}).catch(functiononerror(error) {
  console.error(error.stack);
  res.send('err');
});

Or if you intend to continue the flow at the call, put the .catch handler right there:

co(function *() {
  yield testPromise().then(function(d) {
    console.log(d);
  }).catch(function(error) {
    console.error(error.stack);
  });
  res.send('fin');
});

Alternatively, wrap your promise call in a try-catch:

co(function *() {
  try {
    var d = yieldtestPromise();
    console.log(d);
  } catch(error) {
    console.error(error.stack);
  }
  res.send('fin');
});

Post a Comment for "Reject In Promise Undefined"