我试图依次运行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不必在某些情况下等待代码执行完成。
答案 0 :(得分:3)
您的第三次尝试是要走的路。但是您可以在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')
})
答案 1 :(得分:1)
如果要并行运行,则所有延迟函数都在 Glide.with(context)
.load(gifUrl)
.into(yourImageView)
内部传递。如果要顺序运行,请使用如下所示。
Promise.all
答案 2 :(得分:0)
我将像这样使用数组.reduce
方法:
[delayPromise1, delayPromise2, ...]
.reduce((m, delayPromise) => {
/* here we chain the promises */
return Promise.resolve(m).then(delayPromise)
}, {/* any input data to be the argument of delayPromise1 */} )
.then( res => {
// do something when all is ready
})