[英]Promise.then not executing
In the following code block only 'first promise' is logged to console. 在以下代码块中,仅将“第一个承诺”记录到控制台。 Why is that? 这是为什么? I was trying to write a test to figure out how .then()'s execute after .catch() but was surprised when nothing besides the first promise ran. 我试图编写一个测试来弄清楚.tatch()在.catch()之后的执行情况,但是除了第一个承诺之外什么也没有。 Whats going on here? 这里发生了什么?
function foo() {
return new Promise((resolve, reject) => {
return console.log('first promise')
})
.then(() => console.log('first then'))
.catch(() => console.log('catch block'))
.then(() => console.log('last block'))
.then(() => resolve)
}
foo();
As Yury said, you're not resolving the promise, simply returning a log. 正如Yury所说,你不是在解决这个承诺,只是简单地返回一个日志。
https://jsfiddle.net/k7gL57t3/ https://jsfiddle.net/k7gL57t3/
function foo() {
var p1 = new Promise((resolve, reject) => {
resolve("Test");
})
p1.then(() => console.log('first then'))
.then(() => console.log('last block'))
.then(() => resolve)
.catch(() => console.log('catch block'));
}
foo();
I believe it's because your then
chain does not have closure to resolve
inside of the Promise callback. 我相信这是因为你的then
链没有封闭来resolve
Promise回调内部。 Try this: 尝试这个:
function foo() {
return Promise.resolve()
.then(() => console.log('first then'))
.catch(() => console.log('catch block'))
.then(() => console.log('last block'));
}
or this if you want to use the Promise constructor: 或者如果你想使用Promise构造函数:
function foo() {
return new Promise((resolve, reject) => {
console.log('first promise');
return resolve();
})
.then(() => console.log('first then'))
.catch(() => console.log('catch block'))
.then(() => console.log('last block'));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.