'Does main thread execute the Promise code or does it get added to an js engine api?
Javascript engine executes code in the single main thread.
The main thread will not block on an async function call unless the await keyword is used await fnnane();
When there is an async fucntion like setTimeout or fetch, then it is handed over to the respective Javascript engine api to work on separate to the main thread.
The response (and subsequent function code) is added to the task queue.
But when there is a Promise as shown below, then where does it get passed or does the main thread execute it?
async function foo(i) {
await new Promise(resolve => setTimeout(resolve, 5000));
console.log('Completed foo', i);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
