I have a simple express server listening to POST requests coming in. Each of these 3 possible POST payloads is parsed into a JSON object (partial). One of the objects acts as a "link table" or "link object" because it contains an ID that can be found in 2nd object and another ID that can be found in 3rd object.
My goal is to connect all these 3 objects and their properties and combine them into one object and push it onto an array. I wrote a function to find an index of the object in an array that has the property I am looking for in each iteration so I can update existing object on the global array of merged objects or add a new partial object. I call this function every time I get a new partial POST payload object.
async function findIndexOfElement(array, property, searchParam) { return array.findIndex((el) => { if (el[property]) { return el[property].includes(searchParam); } }); } I also have another asynchronous function that is consuming these objects and poping them off an array. I encountered some sort of race condition because the 3 possible POST payloads do not come always in the same order or at the same time. I didn't post full code that deals with creating and updating objects because it is too long. I have a bug that causes new partial object to be added as standalone objects, instead being merged by finding the common ID. Possibly the error could even be in findIndexOfElement returning wrong index (-1) under some circumstances. I just wish to ask if this is the right design pattern or should I rewrite it differently.
My asynchronous loop is basically:
... let sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); ... async function downloadLoop() { while (true) { await sleep(5000); ... } } But the await inside the while loop does not seem to be waiting. I added the await to fix (ugly, I know) the unpredictability of the POST data coming in, tp not have it start consuming objects from the array while I wait for all the partial POST payloads to come in.
https://stackoverflow.com/questions/65617084/merging-asynchronous-data-and-consuming-it-without-race-conditions-in-nodejs January 08, 2021 at 01:21AM
没有评论:
发表评论