I'm trying to understand better what an async function
in JavaScript is technically, even if I basically know how to use them.
Many introductions to async/await make belive that an async
function is basically just a promise, but that obviously is not the case (at least not with Babel6-transpiled code):
async function asyncFunc() {
// nop
}
var fooPromise = new Promise(r => setTimeout(r, 1));
console.clear();
console.log("typeof asyncFunc is", typeof asyncFunc); // function
console.log("typeof asyncFunc.next is", typeof asyncFunc.next); // undefined
console.log("typeof asyncFunc.then is", typeof asyncFunc.then); // undefined
console.log("typeof fooPromise is", typeof fooPromise); // object
console.log("typeof fooPromise.next is", typeof fooPromise.next); // undefined
console.log("typeof fooPromise.then is", typeof fooPromise.then); // function
Still, it is definitely possible to await
a promise, like await fooPromise()
.
Is a
async funtion
a thing of it's own andawait
is simply compatible with promises?and, is there a way to distinguish between a simple
function
and anasync function
at runtime (in a Babel-compatible way)?