我想保留错误func()
reject
,不直接到onError()
通过选择,
在我总是让func()
resolve
,并确定后的返回结果yield func()
,
如果我想直接前往onError()
use throw ..;
想知道有什么更好的主意我可以让func()
reject
但决定之后yield func()
,直接到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
支持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 https://www.npmjs.com/package/co#examples
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)