Calling async function from sync:
async function wait() {
await new Promise(resolve => setTimeout(resolve, 1000));
return 10;
}
function f() {
// shows 10 after 1 second
wait().then(result => alert(result));
}
f();
Here is an example of how to call an async function in a loop using promise in Node.js using promise all:
const asyncFunction = async () => {
// Do something asynchronous
return 'Hello World';
};
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(asyncFunction());
}
Promise.all(promises).then((results) => {
// Handle the results
});