ccld44
ccld44

Reputation: 109

Is async function a subset of function in JavaScript?

From what I know, async function expression returns an AsyncFunction object.

  1. Does AsyncFunction inherit Function?
  2. Is it okay to use async function in place of function? (e.g. as a callback parameter) If not, what could be a possible pitfall?

Upvotes: 0

Views: 92

Answers (2)

Khauri
Khauri

Reputation: 3863

According the the ECMAScript 2017 Language Specification

The AsyncFunction constructor is the %AsyncFunction% intrinsic object and is a subclass of Function.

Upvotes: 1

Barmar
Barmar

Reputation: 781310

An async function is basically just a function that has been automatically converted to return a promise rather than an ordinary value. It can also use await internally as a shorthand for resolving the promise returned by another async function.

  1. Yes. As shown below, it's type is function, and it's an instance of Function.

async function afunc() {
  return 3;
}

console.log(typeof afunc);
console.log(afunc instanceof Function);

  1. Yes, you can use it as a callback. MDN shows examples of using async functions with setTimeout.

var resolveAfter2Seconds = function() {
  console.log("starting slow promise");
  return new Promise(resolve => {
    setTimeout(function() {
      resolve(20);
      console.log("slow promise is done");
    }, 2000);
  });
};

var resolveAfter1Second = function() {
  console.log("starting fast promise");
  return new Promise(resolve => {
    setTimeout(function() {
      resolve(10);
      console.log("fast promise is done");
    }, 1000);
  });
};

var sequentialStart = async function() {
  console.log('==SEQUENTIAL START==');
  const slow = await resolveAfter2Seconds(); // If the value of the expression following the await operator is not a Promise, it's converted to a resolved Promise.
  const fast = await resolveAfter1Second();
  console.log(slow);
  console.log(fast);
}

var concurrentStart = async function() {
  console.log('==CONCURRENT START with await==');
  const slow = resolveAfter2Seconds(); // starts timer immediately
  const fast = resolveAfter1Second();

  console.log(await slow);
  console.log(await fast); // waits for slow to finish, even though fast is already done!
}

var stillSerial = function() {
  console.log('==CONCURRENT START with Promise.all==');
  Promise.all([resolveAfter2Seconds(), resolveAfter1Second()]).then(([slow, fast]) => {
    console.log(slow);
    console.log(fast);
  });
}

var parallel = function() {
  console.log('==PARALLEL with Promise.then==');
  resolveAfter2Seconds().then((message)=>console.log(message)); // in this case could be simply written as console.log(resolveAfter2Seconds());
  resolveAfter1Second().then((message)=>console.log(message));
}

sequentialStart(); // takes 2+1 seconds in total
// wait above to finish
setTimeout(concurrentStart, 4000); // takes 2 seconds in total
// wait again
setTimeout(stillSerial, 7000); // same as before
// wait again
setTimeout(parallel, 10000); // trully parallel

Upvotes: 5

Related Questions