昨天,我开始学习React并在Promises方面遇到一些麻烦。
var p1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(3)
}, 2000);
})
p1
.then(() => {
setTimeout(() => {
// First block
console.log("This was executed.")
}, 1000)
})
.then(() => {
// Second block
console.log("This block is executed too.");
})
使用以下代码,我如何首先执行第一个块,然后执行第二个块?
最佳答案
您还需要在第一个then
块中返回Promise
var p1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(3)
}, 2000);
})
p1
.then(() => {
return new Promise((resolve, reject) => {
setTimeout(() => {
// First block
console.log("This was executed.")
resolve();
}, 1000)
}
})
.then(() => {
// Second block
console.log("This block is executed too.");
})
关于javascript - 有 promise 和取款的麻烦,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57430787/