K
K
Kusmich2019-12-08 21:44:10
JavaScript
Kusmich, 2019-12-08 21:44:10

How to stop a loop while an async function is running?

There is an array that I go through for-rum, it is necessary to run each element through an asynchronous function and suspend the enumeration of elements until a response is received from the asynchronous function.
How can I pause the loop until the asynchronous function responds?
Example:

const arr = [1,2,3,4,5].forEach(async function(elem, index, arr) {
      // передаем элемент в асинхронную функцию на обработку
            // до момента ответа от асихронной функции необходимо поставить перебор на паузу
            await async(elem)
    })
   function async(elem){
     return new Promise(function(resolve, reject) {
         setInterval(function() {
   		 resolve(true)
          }, 1000); 
      })
   }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
coderisimo, 2019-12-08
@Kusmich

article in the topic - https://habr.com/en/post/435084/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question