Reputation: 40464
So I've always wondered if there is any benefit to this. I'll give examples below.
Asynchronous function wrapped in a Promise:
(async () => {
await new Promise((resolve, reject) => {
fs.writeFile(filePath, dataToWrite, (error) => {
if (error) {
reject(error);
return;
}
resolve();
});
});
})();
Synchronous function:
(() => {
fs.writeFileSync(filePath, dataToWrite);
})();
The only thing I know from light reading is that the synchronous function call blocks the process until done. So for example a web server with api endpoints will not be able to process those requests until the synchronous function is done. Is this also true for the asynchronous function wrapped in a promise? If there are any differences between the two and could one give an explanation if so?
Upvotes: 2
Views: 1636
Reputation: 664548
The synchronous function call blocks the process until done.
Yes.
Is this also true for the asynchronous function wrapped in a promise?
No, that's the point of asynchronous processing.
The await
only "blocks" the execution of the code of the particular async function
until the awaited promise settles, but everything else will continue running.
Upvotes: 3