Koa中的Promise错误处理 [英] Error handling with promises in Koa
问题描述
如果我在Koa中做出承诺,则它们可能会被拒绝:
If I yield promises in Koa, they may be rejected:
function fetch = (){
var deferred = q.defer();
//Some async action which calls deferred.reject();
return deferred.promise;
}
this.body = yield fetch(); //bad, not going to work
Koa中是否有一个错误处理模式来处理此问题,除了用then
显式解开promise并显式处理错误外?
Is there a error-handling pattern in Koa to handle this besides explicitly unwrapping the promise with then
and handling the error explicitly?
推荐答案
尝试/捕获.在引擎盖下 koajs 使用 co </a>.查看co的文档,该文档描述了错误处理要好一些.
Try/Catch. Under the hood koajs uses co. Check out the docs for co, which describes error handling a little better.
function fetch = (){
var deferred = q.defer();
//Some async action which calls deferred.reject();
return deferred.promise;
}
try{
this.body = yield fetch(); //bad, not going to work
}
catch(err){
this.throw('something exploded');
}
这是关于诺言所发生情况的基本示例:
Here is an rudimentary example of what's happening with the promise:
function * someGenerator () {
let result;
try{
result = yield fetch();
console.log('won\'t make it here...');
}
catch(err){
console.log(err);
}
console.log('will make it here...');
}
// Normally co() does everything below this line, I'm including it for
// better understanding
// instantiate the generator
let gen = someGenerator();
// calling gen.next() starts execution of code up until the first yield
// or the function returns.
let result = gen.next();
// the value returned by next() is an object with 2 attributes
// value is what is returned by the yielded item, a promise in your example
// and done which is a boolean that indicates if the generator was run
// to completion
// ie result = {value: promise, done: false}
// now we can just call the then function on the returned promise
result.value.then(function(result){
// calling .next(result) restarts the generator, returning the result
// to the left of the yield keyword
gen.next(result);
}, function(err){
// however if there happened to be an error, calling gen.throw(err)
// restarts the generator and throws an error that can be caught by
// a try / catch block.
// This isn't really the intention of generators, just a clever hack
// that allows you to code in a synchronous style (yet still async code)
gen.throw(err);
});
如果您仍然不确定,那么还有其他几件事可能会有所帮助:
If your still uncertain, here are a couple other things that might help:
在此处观看有关JavaScript生成器的截屏视频: http://knowthen.com/episode-2-understanding- javascript-generators/
Watch my screencast on JavaScript Generators here: http://knowthen.com/episode-2-understanding-javascript-generators/
和/或尝试以下代码:
// test.js
'use strict';
let co = require('co'),
Promise = require('bluebird');
function fetch () {
let deffered = Promise.defer();
deffered.reject(new Error('Some Error'));
return deffered.promise;
}
co.wrap(function *(){
let result;
try{
result = yield fetch();
console.log('won\'t make it here...');
}
catch(err){
console.log(err);
}
console.log('will make it here...');
})();
然后在控制台上运行
$ node test.js
[Error: Some Error]
will make it here...
这篇关于Koa中的Promise错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!