gpt4 book ai didi

javascript - 在循环后运行一个函数

转载 作者:行者123 更新时间:2023-11-30 14:27:55 25 4
gpt4 key购买 nike

我想在 for 循环完成循环之后运行一个函数,但在我的例子中,for 循环之后的函数甚至在循环完成之前运行。这是我的代码

let orderedItems = [];

for (let i = 0; i < orderCode.length; i++) {
menuModel.findOne({
_id: orderCode[i]
}, (err, order) => {
if (order) {
orderedItems.push(order.name);
}
});
}

console.log(orderedItems); // all the tasks below run before the loop finished looping

let orderData = new orderModel();
orderData._id = helpers.createRandomString(5).toUpperCase();
orderData.username = username;
orderData.orderCode = orderCode;
orderData.orderedItems = orderedItems;
orderData.totalPrice = 5;

orderData.save((err) => {
if (err) {
console.log(err);
callback(500, {
'Error': '1'
});
}
callback(200, {
'Message': 'Successfully ordered'
});
});

最佳答案

正如@RishikeshDhokare 所说,一切都是异步执行的。所以诀窍是将任务拆分为单独的函数。

所以首先我们执行一个异步函数,您可以使用 promises 或 async await

然后我们说在所有异步任务完成后执行保存任务。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

const orderCode = [1, 2, 3]
const menuModel = {
findOne: item => new Promise(resolve => resolve({
_id: item._id,
name: 'name'
}))
}

class orderModel {
save(cb) {
return cb(null)
}
}
/*
ignore all above here i'm mocking your funcs
and vars so that the code works
*/

let orderedItems = [];

function getAllOrderedItems() {
const promises = orderCode.map(id => {
return menuModel.findOne({
_id: id
});
})
console.log('...start the request')
// fire all async items then resolve them all at once
return Promise.all(promises)
.then((data) => {
console.log('...finished all requests')
return orderedItems.concat(data);
})
.catch(err => console.log(err))
}

function handleSave(data) {
let orderData = new orderModel();
console.log(data)
console.log('...start save')
orderData.save((err) => {
console.log('...save finished')
});
}
//do all the async tasks then do the save task
getAllOrderedItems()
.then((data) => handleSave(data))

关于javascript - 在循环后运行一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51652306/

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