I want to hold the error from func()
reject
, not direct to onError()
by choise,
Before I always let func()
resolve
, and determine return result after yield func()
,
if I want to direct to onError()
use throw ..;
Wondering any better idea I can just let func()
reject
but detemire after yield func()
, direct to onError()
or not
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();
});
},
co
supports 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);
});
See docs : https://www.npmjs.com/package/co#examples
来源:https://stackoverflow.com/questions/36146468/catch-reject-from-promise