gpt4 book ai didi

javascript - 连续的 promise 没有解决

转载 作者:行者123 更新时间:2023-12-02 21:13:55 26 4
gpt4 key购买 nike

我正在开发一个项目,我使用一些 promise /超时来延迟不同函数的调用。

这个项目的代码有点大,所以我认为最好制作一个示例并测试它并找到导致 promise (只有最后一个)无法解决的原因。

在制作了这段代码的样本后,发生在我身上的同样的错误再次出现,所以当我知道我的逻辑有问题并想寻求帮助时:

我将编写下面的代码并在后面解释:

let i = 0; // declaring i which help us recall the test method (until a given length)

//function 1 2 and 3 are 3 functions that take few seconds to resolve ( made it simpler for you guys
// to understand my problem


function function1() {
return new Promise((res, rej) => {
setTimeout(() => {
console.log("from function 1");
res();
}, 1000);
});
}

function function2() {
return new Promise((res, rej) => {
setTimeout(() => {
console.log("from function 2");
res();
}, 1000);
});
}
//in function 3 if the i didn't reach the length given ( made static here=10) it will finish its code
// then call the test method with the new/incremented i
// this keep happening until i= the required length(10) where the function will return true
// and when the function 3 return back true.. the test function should resolve and call the final()
// function

function function3(i) {
return new Promise((res, rej) => {
setTimeout(() => {
console.log("from function 3");
if (i + 1 == 10) {
res({ bool: true });
} else {
i++;
res({ bool: false, i: i });
}
}, 2000);
});
}

function final() {
console.log("final");
}

function test(i) {
return new Promise((res, rej) => {
function1().then((data) => {
function2().then((data) => {
function3(i).then((data) => {
console.log("boolean: ", data.bool);
if (data.bool) {
console.log("true..so if");
res();
} else {
console.log("false..so else");
test(data.i);
}
});
});
});
}); //end of promise
}

test(i).then(() => {
final();
});

代码运行良好并且连续打印几次:
来自函数 1
来自函数 2
来自函数 3
....

这里是应该调用final函数来打印“final”的地方,但事实并非如此。在调试时,我注意到函数 3 返回 false 9 次,最后一次返回 true,这个 true 应该会导致测试函数解析,但事实并非如此。

最佳答案

现在,在 else 的情况下,您有一个未连接到其他任何东西的悬空 Promise:

      } else {
console.log("false..so else");
test(data.i);
}

当前运行 test 函数在递归调用后永远不会调用其 res,因此它永远无法解决,因此初始调用

test(i).then(() => {
final();
});

永远不会导致最终运行。

虽然您可以通过添加 test(data.i).then(res) 来修复它,以确保 Promise 全部连接:

let i = 0; // delaring i wich help us recall the test method (until a given length)

//function 1 2 and 3 are 3 functions that take few seconds to resolve ( made it simpler for you guys
// to understand my problem


function function1() {
return new Promise((res, rej) => {
setTimeout(() => {
console.log("from function 1");
res();
}, 100);
});
}

function function2() {
return new Promise((res, rej) => {
setTimeout(() => {
console.log("from function 2");
res();
}, 100);
});
}
//in function 3 if the i didnt reach the length given ( made static here=10) it will finish its code
// then call the test method with the new/incremented i
// this keep happening until i= the required length(10) where the function will return true
// and when the function 3 return back true.. the test function should resolve and call the final()
// function

function function3(i) {
return new Promise((res, rej) => {
setTimeout(() => {
console.log("from function 3");
if (i + 1 == 10) {
res({ bool: true });
} else {
i++;
res({ bool: false, i: i });
}
}, 200);
});
}

function final() {
console.log("final");
}

function test(i) {
return new Promise((res, rej) => {
function1().then((data) => {
function2().then((data) => {
function3(i).then((data) => {
console.log("boolean: ", data.bool);
if (data.bool) {
console.log("true..so if");
return res();
} else {
console.log("false..so else");
test(data.i).then(res);
}
});
});
});
}); //end of promise
}

test(i).then(() => {
final();
});

最好避免显式的 Promise 构造反模式,并避免 Promise-as-callback 反模式。这可以通过将 test 设为 async 函数来简洁地完成:

async function test(i) {
await function1();
await function2();
const data = await function3(i);
console.log("boolean: ", data.bool);
if (data.bool) {
console.log("true..so if");
return;
} else {
console.log("false..so else");
await test(data.i);
}
}

let i = 0; // delaring i wich help us recall the test method (until a given length)

//function 1 2 and 3 are 3 functions that take few seconds to resolve ( made it simpler for you guys
// to understand my problem


function function1() {
return new Promise((res, rej) => {
setTimeout(() => {
console.log("from function 1");
res();
}, 100);
});
}

function function2() {
return new Promise((res, rej) => {
setTimeout(() => {
console.log("from function 2");
res();
}, 100);
});
}
//in function 3 if the i didnt reach the length given ( made static here=10) it will finish its code
// then call the test method with the new/incremented i
// this keep happening until i= the required length(10) where the function will return true
// and when the function 3 return back true.. the test function should resolve and call the final()
// function

function function3(i) {
return new Promise((res, rej) => {
setTimeout(() => {
console.log("from function 3");
if (i + 1 == 10) {
res({ bool: true });
} else {
i++;
res({ bool: false, i: i });
}
}, 200);
});
}

function final() {
console.log("final");
}

async function test(i) {
await function1();
await function2();
const data = await function3(i);
console.log("boolean: ", data.bool);
if (data.bool) {
console.log("true..so if");
return;
} else {
console.log("false..so else");
await test(data.i);
}
}
test(i).then(() => {
final();
});

或者,如果您必须继续使用 .thens:

function test(i) {
return function1()
.then(function2)
.then(() => function3(i))
.then((data) => {
console.log("boolean: ", data.bool);
if (data.bool) {
console.log("true..so if");
return;
} else {
console.log("false..so else");
return test(data.i);
}
});
}

let i = 0; // delaring i wich help us recall the test method (until a given length)

//function 1 2 and 3 are 3 functions that take few seconds to resolve ( made it simpler for you guys
// to understand my problem


function function1() {
return new Promise((res, rej) => {
setTimeout(() => {
console.log("from function 1");
res();
}, 100);
});
}

function function2() {
return new Promise((res, rej) => {
setTimeout(() => {
console.log("from function 2");
res();
}, 100);
});
}
//in function 3 if the i didnt reach the length given ( made static here=10) it will finish its code
// then call the test method with the new/incremented i
// this keep happening until i= the required length(10) where the function will return true
// and when the function 3 return back true.. the test function should resolve and call the final()
// function

function function3(i) {
return new Promise((res, rej) => {
setTimeout(() => {
console.log("from function 3");
if (i + 1 == 10) {
res({ bool: true });
} else {
i++;
res({ bool: false, i: i });
}
}, 200);
});
}

function final() {
console.log("final");
}

function test(i) {
return function1()
.then(function2)
.then(() => function3(i))
.then((data) => {
console.log("boolean: ", data.bool);
if (data.bool) {
console.log("true..so if");
return;
} else {
console.log("false..so else");
return test(data.i);
}
});
}
test(i).then(() => {
final();
});

每当你有一个 Promise 时,你几乎总是应该返回它,或者作为脚本的初始异步操作。

关于javascript - 连续的 promise 没有解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61022756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com