An asynchronous function is a function that returns a promise. This helps you in cases where you have several asynchronous actions occurring one after another:
function asyncFunc() { return doSomethingAsync() // doSomethingAsync() returns a promise .then(() => { // do some stuff return doSomethingElseAsync(); // returns a promise }) .then(something => { // do some stuff return doSomethingElseEntirelyAsync(something); // returns a promise }); }
Turns on
async function asyncFunc() { await doSomethingAsync();
It reads much better, and you can use regular tools like try / catch and work with them for loops even if they are asynchronous.
Asynchronous functions are NOT replaced with promises, they are sugars on top of them, to handle specific cases where you have many sequential asynchronous actions.
Since await is basically just “waiting for this promise”, you can still use cool aggregation methods like Promise.all() and Promise.race() and wait for the result of several (or the first of several) promises.
I am not familiar with how to distinguish between two at runtime, because, like classes, asynchronous functions are just sugar on top of promises. (Although there may be hacks, like using the .toString function, and analyze the results, I do not consider them).
Madara uchiha
source share