As an ex COBOL programmer I struggle with promises but I can get simple promises to work. Can you please help with the following: I have function that loops round a collection and update some of the items. As it stands, the function returns after the loop - console.log(“end of loop2”) but the saves return later. I want it only to return after all the updates have taken place.
- console log -
end of promise
reset_lunch_diagnostics promise
save Alan Mellor
save York Ride
export function reset_lunch_diagnosticsC(rid, grpSize){ //during testing reset Eve of ride updates etc
return new Promise( function (resolve, reject){
var update = false
console.log(“entry into reset_lunch_diagnostics” )
return wixData.query(“Lunches”)
.eq(“rideId”, rid)
.ge(“status”,0)
.find()
.then( (Lresults) => {
var cntT = 0
Lresults.items.forEach((item3)=> {
if (item3.status > 1) {++cntT}
})
console.log(“start of loop2”, Lresults.items.length)
Lresults.items.forEach((item3)=> {
if (item3.choice === “*** not now coming ***”) {update = true ; item3.choice = “Soup of the day” }
if (item3.status > 4) {update = true ; item3.status = 4}
else if (item3.status === 3 && cntT > grpSize) {update = true ; item3.status = 1; --cntT}
else if (item3.status === 4 && cntT > grpSize) {update = true ; item3.status = 2; --cntT}
if (update) wixData.save(“Lunches”, item3).then((lItem) => {console.log(“save”, lItem.name)})
})
console.log(“end of loop2”)
console.log(“end of promise”)
resolve(“done”)
})
})
}