Promise.all 没有回来
Promise.all not returning
我一直在尝试让 Promise.all 使用承诺列表但没有成功,所以只尝试使用一个承诺的数组来代替,但我遇到了同样的问题:
let tasks = [];
tasks.push(function(resolve, reject){
superagent
.post(URL_ROOT + url_path)
.send(data)
.end(function(err, res){
if(err)
reject(err);
assert.equal(res.status, status.UNAUTHORIZED); //401
console.log('Promise completed successfully');
resolve();
});
});
Promise.all([
new Promise(tasks[0])
]).then( function(){
console.log("Done");
done();
})
.catch( function(reason){
throw new Error(reason);
});
"Promise completed successfully" 打印得很好,但它只是挂起,而 'Done' 从不打印。
如有任何帮助,我们将不胜感激。
查看节点处理程序:
.end(function(err, res){
if(err)
reject(err);
assert.equal(res.status, status.UNAUTHORIZED); //401
console.log('Promise completed successfully');
resolve();
});
如果出现错误(并且断言成功),这将同时调用 reject
和 resolve
。我怀疑你的情况是这样,所以 promise 被拒绝了(因为 reject 在 resolve 之前被调用),代码继续并打印 Promise completed successfully
.
之后,承诺链遇到拒绝处理程序:
.catch( function(reason){
throw new Error(reason);
});
但是这段代码没有做任何事情,因为投入一个 promise continuation 将转化为对结果 promise 的拒绝,这里忘记了这个 promise。
尝试以下方法来验证我的理论,看看它是否记录:
.catch( function(reason){
console.log("Promise rejected");
throw new Error(reason);
});
要解决这个问题,您所要做的就是稍微重构一下代码:
.end(function(err, res){
if(err) {
reject(err);
} else {
resolve();
assert.equal(res.status, status.UNAUTHORIZED); //401
console.log('Promise completed successfully');
}
});
因此您已将异步任务转换为适当的承诺(可能还必须处理 .on("error", reason => reject(reason))
),并在 catch 子句中放置错误处理程序。
如果您仍想将错误传递给全局错误处理程序,最好的办法是在 setTimeout
中进行,这样 promise 回调就无法捕获并翻译错误:
.catch( function(reason) {
setTimeout(() => {
throw new Error(reason);
}, 0);
});
我一直在尝试让 Promise.all 使用承诺列表但没有成功,所以只尝试使用一个承诺的数组来代替,但我遇到了同样的问题:
let tasks = [];
tasks.push(function(resolve, reject){
superagent
.post(URL_ROOT + url_path)
.send(data)
.end(function(err, res){
if(err)
reject(err);
assert.equal(res.status, status.UNAUTHORIZED); //401
console.log('Promise completed successfully');
resolve();
});
});
Promise.all([
new Promise(tasks[0])
]).then( function(){
console.log("Done");
done();
})
.catch( function(reason){
throw new Error(reason);
});
"Promise completed successfully" 打印得很好,但它只是挂起,而 'Done' 从不打印。
如有任何帮助,我们将不胜感激。
查看节点处理程序:
.end(function(err, res){
if(err)
reject(err);
assert.equal(res.status, status.UNAUTHORIZED); //401
console.log('Promise completed successfully');
resolve();
});
如果出现错误(并且断言成功),这将同时调用 reject
和 resolve
。我怀疑你的情况是这样,所以 promise 被拒绝了(因为 reject 在 resolve 之前被调用),代码继续并打印 Promise completed successfully
.
之后,承诺链遇到拒绝处理程序:
.catch( function(reason){
throw new Error(reason);
});
但是这段代码没有做任何事情,因为投入一个 promise continuation 将转化为对结果 promise 的拒绝,这里忘记了这个 promise。
尝试以下方法来验证我的理论,看看它是否记录:
.catch( function(reason){
console.log("Promise rejected");
throw new Error(reason);
});
要解决这个问题,您所要做的就是稍微重构一下代码:
.end(function(err, res){
if(err) {
reject(err);
} else {
resolve();
assert.equal(res.status, status.UNAUTHORIZED); //401
console.log('Promise completed successfully');
}
});
因此您已将异步任务转换为适当的承诺(可能还必须处理 .on("error", reason => reject(reason))
),并在 catch 子句中放置错误处理程序。
如果您仍想将错误传递给全局错误处理程序,最好的办法是在 setTimeout
中进行,这样 promise 回调就无法捕获并翻译错误:
.catch( function(reason) {
setTimeout(() => {
throw new Error(reason);
}, 0);
});