Nodejs Promise()错误吞咽
Nodejs Promise() error swallowing
我正在为承诺而苦苦挣扎
具有以下代码段:
Promise.resolve()
.then(a("OK1"))
.then(a("OK2"))
.then(a("OK3"))
.then(a("OK4"))
.then(a("OK5"))
.then(function() {
console.log("FINISH");
})
.catch(function(e) {
console.log("ERROR: " + e);
});
function a(i) {
return new Promise(function(resolve, reject) {
if(i == "OK4") {
console.log("THROW");
throw('Error'); //This does not happen
reject();
return;
}
if(i == "OK2") {
reject()
return;
}
console.log(i);
resolve();
});
}
我希望能够拒绝承诺 - 然后执行下一个 .then
("OK3")
但我无法抛出异常 - 我想在 "OK4"
上触发 .catch
OK4 - 被拒绝并且没有输出,但是 OK5 仍然被执行 - 有没有办法绕过它?
这将是我的预期输出:
OK1
OK3
ERROR: ....
正如 Bergi 提到的,您传递给每个 .then
运行 函数的参数立即和 return 一个承诺。承诺链需要一个函数的引用,它可以在以后运行。
如果您想 运行 您的函数带有一个参数,您可以将其包装在一个匿名函数中,这样 promise 链可以稍后 运行 匿名函数。
a("OK1")
.then((res)=>{
return a("OK2")
})
.then((res)=>{
return a("OK3")
})
.then((res)=>{
return a("OK4")
})
.then((res)=>{
return a("OK5")
})
.then(()=>{
console.log("FINISH");
})
.catch(function(e) {
console.log("ERROR: " + e);
});
function a (i) {
return new Promise(function(resolve, reject) {
if(i == "OK4") {
console.log("THROW");
throw new Error(i); //This does not happen
}
if(i == "OK2") {
return reject(new Error(i))
}
setTimeout(function () {
console.log(i);
resolve(i);
}, 100)
});
}
可以传递最终将 return 承诺的函数。该函数将传递先前承诺解决的值。
b(1)
.then(b)
.then(b)
.then(b)
.then((res) => {
console.log("FINISH",res);
})
.catch(function (e) {
console.log("ERROR: " + e);
});
function b (i) {
return new Promise(function (resolve, reject) {
console.log(i)
resolve(i+1)
})
}
我正在为承诺而苦苦挣扎
具有以下代码段:
Promise.resolve()
.then(a("OK1"))
.then(a("OK2"))
.then(a("OK3"))
.then(a("OK4"))
.then(a("OK5"))
.then(function() {
console.log("FINISH");
})
.catch(function(e) {
console.log("ERROR: " + e);
});
function a(i) {
return new Promise(function(resolve, reject) {
if(i == "OK4") {
console.log("THROW");
throw('Error'); //This does not happen
reject();
return;
}
if(i == "OK2") {
reject()
return;
}
console.log(i);
resolve();
});
}
我希望能够拒绝承诺 - 然后执行下一个 .then
("OK3")
但我无法抛出异常 - 我想在 "OK4"
OK4 - 被拒绝并且没有输出,但是 OK5 仍然被执行 - 有没有办法绕过它?
这将是我的预期输出:
OK1
OK3
ERROR: ....
正如 Bergi 提到的,您传递给每个 .then
运行 函数的参数立即和 return 一个承诺。承诺链需要一个函数的引用,它可以在以后运行。
如果您想 运行 您的函数带有一个参数,您可以将其包装在一个匿名函数中,这样 promise 链可以稍后 运行 匿名函数。
a("OK1")
.then((res)=>{
return a("OK2")
})
.then((res)=>{
return a("OK3")
})
.then((res)=>{
return a("OK4")
})
.then((res)=>{
return a("OK5")
})
.then(()=>{
console.log("FINISH");
})
.catch(function(e) {
console.log("ERROR: " + e);
});
function a (i) {
return new Promise(function(resolve, reject) {
if(i == "OK4") {
console.log("THROW");
throw new Error(i); //This does not happen
}
if(i == "OK2") {
return reject(new Error(i))
}
setTimeout(function () {
console.log(i);
resolve(i);
}, 100)
});
}
可以传递最终将 return 承诺的函数。该函数将传递先前承诺解决的值。
b(1)
.then(b)
.then(b)
.then(b)
.then((res) => {
console.log("FINISH",res);
})
.catch(function (e) {
console.log("ERROR: " + e);
});
function b (i) {
return new Promise(function (resolve, reject) {
console.log(i)
resolve(i+1)
})
}