在for循环中使用Promise,所有后来的.then总是被调用,直到所有第一个.then完成运行,为什么会这样?<



我对JavaScript和Promise的概念不熟悉。我正在做测试。

我创建了一个promise和两个for循环,总共运行promise 6次,我知道它们何时被console.log()执行

这是我的代码

console.log("testing promise")
let p = new Promise((resolve, reject)=> { //promise take a function as parameter.
console.log("defining variable a ")
let a = 2   
if(a==2){
console.log("inthe a ==2 block")
resolve("success")
}
else if (a==3){
reject("fail")
}
else if(a==5){
reject("fail when a =5")
}

})
console.log("before going into for loop")
for(let i=0; i<3; i++){
p.then((message)=>{
console.log("the message is "+message);
return("second message")
}).then((ak)=>{
console.log("the message is "+ak);
return false
}).catch((message)=>{
console.log("the message is "+message)
})
.finally(()=>{
console.log("finially block")
})
}
for(let i=0; i<3; i++){
p.then((message)=>{
console.log("the message is "+message);
return("second message")
}).then((ak)=>{
console.log("the message is "+ak);
return false
}).catch((message)=>{
console.log("the message is "+message)
})
.finally(()=>{
console.log("finially block")
})
}

,控制台是这样的:

testing promise
script.js:5 defining variable a 
script.js:8 inthe a ==2 block
script.js:21 before going into for loop
3 script.js:25 the message is success
3 script.js:41 the message is success
3 script.js:28 the message is second message
3 script.js:44 the message is second message
3 script.js:34 finially block
3 script.js:50 finially block

为什么第二个。then和所有的。finally会等待每一个单独的first .then和then被执行?

我知道我可能在我的知识数据库中丢失了一些基本概念在Javascript存储在我的大脑:),请让我知道什么概念在JS将是关于这个问题,我会看一看,谢谢!

如果你注意到,在你的例子中,你引用/分配了一个promise给一个变量(let p = new Promise((resolve, reject)=> {....),然后你使用了变量p,现在在你的for循环中有了promise的相同的实例的引用。

请记住,一个promise的引用,一旦它被实现,将总是返回相同的结果。例如:

const aPromise = new Promise(resolve => resolve(Math.random());
// loop simulation
for (let x = 0; x < 5; x++) {
// this will print the same generated random number 5 times.
aPromise.then(result => console.log(result));
}

这可能就是您在console.log()中看到的内容的原因。基本上,两个for循环异步执行对promise的相同引用,其中每个处理程序序列(。然后(Then)和(finally)同时(或几乎)完成。

为了正确地演示你想要实现的目标,你需要在每次迭代中执行一个Promise的新实例。另一个例子:


// use a function here instead to trigger a new instance of a promise.
let aPromise = (loopReference) => new Promise(resolve => {
// generates a 2 digit random number.
const randomTimeout = (Math.floor(Math.random() * 90 + 10));

const returnValue = `${loopReference} ${randomTimeout}`;
console.log(`generated value is: ${returnValue}`);

// simulate a process by using a timeout based on the generated random value.
setTimeout(() => {
resolve(returnValue);
}, randomTimeout);
});

// first loop
for (let x = 0; x < 5; x++) {
aPromise('first loop').then(val => {
console.log('first then for: ' + val);
return val;
}).then(val => {
console.log('second then for: ' + val);
return val;
});
}
// second loop
for (let x = 0; x < 5; x++) {
aPromise('second loop').then(val => {
console.log('first then for: ' + val);
return val;
}).then(val => {
console.log('second then for: ' + val);
return val;
});
}

你应该看到这样的日志:

generated value is: first loop 46
generated value is: first loop 99
generated value is: first loop 75
generated value is: first loop 56
generated value is: first loop 43
generated value is: second loop 60
generated value is: second loop 50
generated value is: second loop 58
generated value is: second loop 68
generated value is: second loop 35
first then for: second loop 35
second then for: second loop 35
first then for: first loop 43
second then for: first loop 43
first then for: first loop 46
second then for: first loop 46
first then for: second loop 50
second then for: second loop 50
first then for: first loop 56
second then for: first loop 56
first then for: second loop 58
second then for: second loop 58
first then for: second loop 60
second then for: second loop 60
first then for: second loop 68
second then for: second loop 68
first then for: first loop 75
second then for: first loop 75
first then for: first loop 99
second then for: first loop 99

如果你明白了,请告诉我。好运!

相关内容