I would like to propose "zero-overhead async/await" syntax. Zero-overhead would essentially mean that async code is achieved by a single callback, instead of current Promise-based code which is 2 callbacks + object.
The syntax would very much look similar to the existing async/await syntax but would operate on top of callbacks instead of promises.
Await a callback-powered async function:
await cb fs.readFile('a.txt.', cb);
Await callback-powered async function, where the last "callback" argument is implicit:
await _ fs.readFile('a.txt');
Create an async function powered by callback, note the async
keyword in place of the callback
argument:
function readFile(filename, async) {
// ...
return await _ napi.binding.read(filename);
}
The above readFile
method, can be called using the regular callback-based JavaScript:
readFile('a.txt.', (error, value) => {
if (error) {
// ...
} else {
// ...
}
});
Or using the zero-overhead callback-based async/await (when in async context):
try {
let value = await _ readFile('a.txt');
// ...
} catch (error) {
// ...
}
More on the proposal: Zero-overhead Async/Await - DEV Community