Y
Y
yopive35622020-12-27 23:55:09
Node.js
yopive3562, 2020-12-27 23:55:09

How to wait for the execution of a cyclic function?

Good day! City of some wild crutch, as it seems to me.
The goal is to wait for the execution of the data in batches and, based on the results, do something. You can't just use await'om some function there.
I tried to do it through this construction:
When data arrives, we add it to the pool of the current iteration and wait for the promise after requestPull[currentRequestNumber]["promise"]. After that, we pull out the result that belongs to the data.
The problem is that sometimes new data arrives in the process of execution. They are added to the current pool, but the data is already being processed. Because of this, they will not be in the cut. When returning a promise, when trying to get a result with this data, it will be undefined. And that data is lost.
How to transform the structure or how to redo everything? (

requestPull[currentRequestNumber] = {
            "currentRequestNumber": currentRequestNumber, //текущий номер итерации
            "promise": run(), //возвращает промис при выполнение
            "data": [] //входные данные
            "result": {} //результат выполнение run() для каждых данных
        }


upd1:
Let's say data comes every second. The data cannot be processed separately. Processing of all incoming data takes 2+3 seconds.
When processing starts, some actions occur for the first two seconds. After that, all the data is taken and processing begins. All data received after three seconds will be lost.
The promise will work after the end of processing ^5 seconds (processing will end)^, but there will be no results for such data.
All numbers are conditional.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question