-4
const somefunction = async () => {
  const arr = [...]
  for(let i = 0 ; i < arr.length ; ++i){
    await updater(arr[i])
  }
}

The above for loop will wait for the promise to resolve before moving to the next iteration, but, is this now a blocking action for the main event loop in nodejs?

please note: this is a "for" not a "forEach" loop. Very different.

  • 2
    Does this answer your question? [Using async/await with a forEach loop](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop) – Tobias Schäfer Jul 12 '20 at 08:41
  • No not at all @TobiasSchäfer –  Jul 12 '20 at 08:47
  • that question specifically refers to forEach which is completely different to a for loop –  Jul 12 '20 at 08:48

2 Answers2

0

No, it isn't blocking.

async functions go to sleep while they are awaiting a promise. Control is handed back to the calling function (which gets the unresolved Promise returned from the async function).

(Of course, if updater is blocking then it will still block).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • However a for loop processing large arrays will be deemed blocking no? –  Jul 12 '20 at 08:52
  • @John — Not if "processing" means `await`ing a Promise which is actually asynchronous. – Quentin Jul 12 '20 at 08:56
  • ok thanks. I cannot find any docs specifically on what you said, do you happen to know any. I keep landing https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of which i don't think is correct –  Jul 12 '20 at 09:02
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await – Quentin Jul 12 '20 at 09:05
  • But this does not explain what happens to the for loop as we are awaiting the response of the promise "updater" or what affect it may have on the main thread. –  Jul 12 '20 at 09:07
  • 1
    Quote: "The await expression causes async function execution to pause until a Promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfillment… allowing the caller of the await's function to resume execution before the deferred continuation of the await's function. After the await defers the continuation of its function, if this is the first await executed by the function, immediate execution also continues by returning to the function's caller a pending Promise for the completion of the await's function and resuming execution of that caller" – Quentin Jul 12 '20 at 09:10
0

As you can see by running this example, the function will be non-blocking. This is the nature of async await.

    function asyncAction(message) {
        console.log("asyncAction start: " + message)
        return new Promise ( (res, err) => {
                                            setTimeout( () => res("done"),2000)
                                            }
        )
        console.log("asyncAction complete: " + message)
    }   

    async function asyncActions() {
        await asyncAction("1")
        console.log("1 done")
        await asyncAction("2")
        console.log("2 done")
        await asyncAction("3")              
        console.log("3 done")
    }   

    console.log("program start")
    asyncActions()
    console.log("program complete")
crosen9999
  • 823
  • 7
  • 13
  • Yes this demonstrates what the async does but the question is more specifically related to how it behaves within a for loop. –  Jul 12 '20 at 08:51