Background
I have a problem where my promise is not being returned to its calling function. I know this would usually work, but it's not when inside a recursive function. The resolve is being called and I can see the function FindElementById is finding the element correctly, however the call in the thenable UpdateMasterMenuItem(result, queuedItem) is not being called.
What am I doing in my code snippets, what am I trying to do?
Loop over a collection queuedItems and find those items in another collection menuItems. I do this by passing in the Id of the item I'm trying to find, as well as the collection it's in, into a recursive function FindElementById. When the item is found, I'm returning the found item and doing some other stuff to it in the function UpdateMasterMenuItem.
Code
// Calling Loop
queuedItems.forEach(function (queuedItem) {
FindElementById(queuedItem.dataset.id, menuItems).then(function(result) {
UpdateMasterMenuItem(result, queuedItem);
});
});
// Recursive Function
function FindElementById(id, menuItems) {
return new Promise((resolve) => {
menuItems.forEach(function (menuItem) {
if (menuItem.Id === id) {
return resolve(menuItem);
} else if (menuItem.Child.length > 0) {
FindElementById(id, menuItem.Child);
}
});
});
}
FindElementByIdreturns a promise? There's nothing asynchronous going on in the method.