我试着用co做一些节点脚本。它工作得很好,但在脚本完成之前有很大的延迟。(我在一秒后得到"Ok“或"Bad”,但脚本在它之后7秒就结束了)。我错过了什么?
co(function *() {
let errors = yield someCheck(process.argv);
if (!errors) {
console.log('Ok');
} else {
console.log('Bad');
}
})(function(e) {
if (e) {
console.log(e);
}
});发布于 2016-06-07 22:52:36
当我运行你的代码时,我得到了一个typeError。我不知道你要做什么,但我认为你不能在调用co()时将错误处理程序作为第二个参数传递,你必须使用then()或catch()来处理错误。
// 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);
}发布于 2016-06-14 15:11:40
我想process.exit()会解决你的问题的。
https://stackoverflow.com/questions/37682144
复制相似问题