我正在使用蓝鸟在我的自定义库中实现promisify。我有一本书
用户,lib-1,lib-2等
在用户注册时,从我的用户路由器
// user_router.js
var user = new User();
var lib1 = new Lib1();
var lib2 = new Lib2();
// I am expecting, all method will be async by following 3 lines!
Promise.promisifyAll(user);
Promise.promisifyAll(lib1);
Promise.promisifyAll(lib2);
user.validationAsync(payload)
.then(function(result_from_validation) {
return lib1.validationAsync(payload);
})
.then(function(result_lib1_validation)) {
return lib2.validationAsync(payload)
})
.then(function(result_lib2_validation)) {
// final save
user.registerAsync(payload);
lib1.saveAsync(payload);
lib2.saveAsync(payload);
return {'response-data'}
})
.then(function(response)) {
// send response
res.json('success', 'User successfully created', {});
})
.catch(function(error)) {
//all error will be handled here
//these error will be either throw or by promise rejected.
res.json('error', error.message, {error.stack});
})在验证方法中,我抛出的错误如下
//user_lib.js
// This throw will be handled by router catch. ???
throw Error('Field value is invalid');在保存方法中,mongooes模式也有一些验证,比如用户已经存在,所以无法创建用户。
function register (payload)
{
// payload data binded on user_modal here
var user_modal = getUserModal(payload);
user_modal.saveAsync()
.then(function(){})
.catch(function(error){
//How to handle error here and send this error to router
})
}以上都只是我的期望。我想发送来自user_router.js的所有回复。(我的用户模块已经实现了。现在需要通过蓝知更鸟来集成它)
现在的问题::
发布于 2015-07-01 07:38:29
现在,我使用try进行正常语句,并使用error进行异步错误处理!!:)非常感谢guyz
https://stackoverflow.com/questions/31130927
复制相似问题