I have the following:
https://jsfiddle.net/qofbvuvs/
var allItems = ["1", "2", "3"]
var allPeople = ["A", "B"]
var testFoo = function(itemValue, peopleValue) {
setTimeout(function(){
return itemValue == "3" && peopleValue == "B"
}, 200)
}
allItems.forEach(function(itemValue) {
allPeople.forEach(function(peopleValue) {
// I want to iterate through each object, completing testFoo before moving on to the next object, without recursion. TestFoo has a few instances of setTimeout. I've tried using promises to no avail.
if (testFoo(itemValue, peopleValue)){
alert("success")
} else{
// nothing
};
})
})
alert("complete")
My goal is to iterate through each item, one by one, in order, while waiting for the results of testFoo
. If testFoo passes, then I should stop execution.
I've tried to use promises (https://jsfiddle.net/qofbvuvs/2/) but can't get the behavior I'm looking for. Success
should be called before Complete
. TestFoo
has a couple setTimeouts that I need to work around (it's a library I can't modify). How can this be achieved?