0

我想保留错误func() reject,而不是直接onError()选择,

之前我总是让func() resolve,然后确定返回结果yield func()
如果我想直接onError()使用throw ..;

想知道任何更好的想法我可以让func() reject但决定之后yield func(),直接onError()或不

co(function* () {
  yield func();
  // if reject catch here, not direct to onError 


  yield func();
  // if reject don't catch here just direct to onError

}).then(function (response) {
  response = JSON.stringify(response);
  res.send(response);
}, function (err) {
  onError(err);
});


// ...
func: function() {
  return new Promise(function (resolve, reject){
    ...
    reject();
  });
},
4

1 回答 1

1

co支持try/catch

co(function* () {
  try{
      yield func();
  }
  catch {
     // if reject catch here, not direct to onError 
  }




  yield func();
  // if reject don't catch here just direct to onError

}).then(function (response) {
  response = JSON.stringify(response);
  res.send(response);
}, function (err) {
  onError(err);
});

请参阅文档:https ://www.npmjs.com/package/co#examples

于 2016-03-22T05:32:22.463 回答