gpt4 book ai didi

node.js - 如何在使用promise时分别运行多个函数?

转载 作者:太空宇宙 更新时间:2023-11-03 22:48:53 25 4
gpt4 key购买 nike

我正在尝试按顺序运行 4 个函数,我已经尝试了下面的代码,但在运行第一个和第二个函数后,函数如何卡住了

代码示例:

const Bluebird = require('bluebird');

///// also tried changing all Bluebird below to Promise -> didn't work

//const Promise = require('bluebird');

const promisesFunc = async (array) => {
let interval = 1000;
const delayPromise1 = (data, delayDuration) => {
return new Promise((resolve) => {
setTimeout(() => {
/// do some code here requires .map function may take 10s or more
resolve();
}, delayDuration)
});
};

const delayPromise2 = (data, delayDuration) => {
return new Promise((resolve) => {
setTimeout(() => {
/// do some code here requires .map function may take 10s or more
resolve();
}, delayDuration)
});
};

const delayPromise3 = (data, delayDuration) => {
return new Promise((resolve) => {
setTimeout(() => {
/// do some code here requires .map function may take 10s or more
resolve();
}, delayDuration)
});
};

const delayPromise4 = (data, delayDuration) => {
return new Promise((resolve) => {
setTimeout(() => {
/// do some code here requires .map function may take 10s or more
resolve();
}, delayDuration)
});
};

try {
/////////////// first attempt //////////////
await Bluebird.map(array, (data, index) => delayPromise1(data, index * interval))
await Bluebird.map(array, (data, index) => delayPromise2(data, index * interval))
await Bluebird.map(array, (data, index) => delayPromise3(data, index * interval))
await Bluebird.map(array, (data, index) => delayPromise4(data, index * interval))
console.log('done ***************************************************');
setTimeout(() => {
console.log('response was sent');
res.status(200).json('done')
}, 1000);

/////////////// second attempt ////////////

const promises = Bluebird.map(array, (data, index) => delayPromise1(data, index * interval))
.then(() => Bluebird.map(array, (data, index) => delayPromise2(data, index * interval)))
.then(() => Bluebird.map(array, (data, index) => delayPromise3(data, index * interval)))
.then(() => Bluebird.map(array, (data, index) => delayPromise4(data, index * interval)))
.then(() => {
setTimeout(() => {
console.log('response was sent');
res.status(200).json('done')
}, 1000);
})
.catch(err => console.error(err))

await Promise.all([promises]);

///////////// third attempt ////////////////////

const promises1 = array.map((data, index) => delayPromise1(data, index * interval));
const promises2 = array.map((data, index) => delayPromise2(data, index * interval));
const promises3 = array.map((data, index) => delayPromise3(data, index * interval));
const promises4 = array.map((data, index) => delayPromise4(data, index * interval));
await Promise.all([promises1, promises2, promises3, promises4]);
setTimeout(function(){
console.log('response was sent');
res.status(200).json('done')
}, 1000);


} catch (e) {
console.error(e);
}
}
promisesFunc(array)

注意在第一次和第二次尝试中,delayPromise1和delayPromise2函数成功运行,但随后停止并且不继续,其他解决方案分别不起作用

知道为什么会发生这种情况吗?是否有更好的解决方案来保证这些函数分别运行。

PS函数结构需要像这样。并且需要分别按顺序运行。

映射到数据数组中,然后在delayPromise函数中对其进行一些处理,然后resolve()。在某些情况下,解决 promise 不必等待代码执行完成。

最佳答案

你的第三次尝试是正确的选择。但是,您可以在 Promise.all 解析并且所有异步操作完成后发送响应。

const promises1 = array.map((data, index) => delayPromise1(data, index * interval));
const promises2 = array.map((data, index) => delayPromise2(data, index * interval));
const promises3 = array.map((data, index) => delayPromise3(data, index * interval));
const promises4 = array.map((data, index) => delayPromise4(data, index * interval));

await Promise.all([promises1, promises2, promises3, promises4]).then(() => {
console.log('response was sent');
return res.status(200).json('done')
})

关于node.js - 如何在使用promise时分别运行多个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57783083/

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