【发布时间】:2018-09-03 10:09:54
【问题描述】:
¿在 nodeJS 中使用 async/await 实现 promise 错误处理程序 .catch((err)=>{}) 的最佳实践是什么?
代码运行在 nodeJS V8.10.0
这是我实施后者的失败尝试。
考虑以下代码:
使用.catch((err)=>{})
//Function that rejects a promise with a probability of 50%
function randomReject(resolve,reject){
setTimeout(()=>{
if(Math.random() > 0.5)
resolve('Bigger than 1/2');
else
reject('Smaller than 1/2');
},500)
}
//Test promises
//THESE PROMISES ARE DECLARED INCORRECTLY
var promise1 = new Promise((resolve,reject)=>{ //HERE IS THE PROBLEM
randomReject(resolve,reject);
})
var promise2 = new Promise((resolve,reject)=>{ //HERE IS THE PROBLEM
randomReject(resolve,reject);
})
//EXPLANATION: Without a wrapper function around the promises, these will
//run in place as soon as they are declared. And since there is no promise
//rejection handler declared in the latter code these will cause an
//"UnhandledPromiseRejectionWarning" in NodeJS.
//Async function declaration
async function asyncFnc2(){
var res1 = await promise1;
var res2 = await promise2;
return res1 + ' ' +res2;
}
//Async function call
asyncFnc2().then((val)=>{
console.log(val);
}).catch((err)=>{
console.log(err);
})
控制台输出指示未处理的承诺拒绝。
非常欢迎任何指向正确方向的指针,以及代码中反模式或不良做法的更正。
提前感谢您的时间和兴趣。
分辨率
function randomReject(resolve,reject){
setTimeout(()=>{
if(Math.random() > 0.5)
resolve('Bigger than 1/2');
else
reject('Smaller than 1/2');
},500)
}
//Test promises
//These promises are wrapped around functions so they are not run in place
function promise1(){
return new Promise((resolve,reject)=>{
randomReject(resolve,reject);
})
}
function promise2(){
return new Promise((resolve,reject)=>{
randomReject(resolve,reject);
})
};
//These promises are wrapped around functions so they are not run in place
//No need for the "try catch", just let the async function do the dirty
//work.
async function asyncFnc2(){
var res1 = await promise1();
var res2 = await promise2();
return res1 + ' ' +res2;
}
//Any exception will be automatically catch by the ".catch((err)=>{})"
asyncFnc2().then((val)=>{
console.log(val);
}).catch((error)=>{
console.log(error);
})
【问题讨论】:
-
我认为你对 Promise 的工作原理有点困惑,..
var promise1 = new Promise((resolve,reject)=>{,这是一个 Promise,它会立即执行。然后你有async function asyncFnc2(){等待promise1和promise2,已经开始了,promise3甚至从未等待。通常当你创建一个新的 Promise 时,你会从另一个函数中调用它。例如..function test() { return new Promise(resolve, reject) {}}等。
标签: javascript node.js async-await es6-promise