Sample Script for Executing with Synchronous Process using Node.js

Gists

This is a sample script for executing with the synchronous process using Node.js.

Sample script

function work(e) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log(e);
            resolve("ok" + e);
        }, 1000);
    });
}

async function main() {
    var ar = [1, 2, 3, 4, 5];
    for (var i = 0; i < ar.length; i++) {
        console.log('start' + ar[i]);
        await work(ar[i]).then((res) => console.log(res));
        console.log('end' + ar[i]);
    }
}

main(); // Run main().

Result

start1
1
ok1
end1
start2
2
ok2
end2
start3
3
ok3
end3
start4
4
ok4
end4
start5
5
ok5
end5

 Share!