Search code examples
javascriptnode.jspromiseasync-awaites6-promise

Are async functions just for IO blocking operations?


I have some functions that do a lot of calculations with data and arrays but all in memory, it does not do any IO. I've been reading trying to get into nodejs, I was wondering if there is any benefit on making then async? For example, function a:

function a(b, c, d) {
  // a lot of operations with arrays
  return value;
}

Does it make any sense to return a promise in this case if it does not involve any IO blocking operation? For example:

function a(b, c, d) {
  return new Promise(resolve, reject) {
    // a lot of operations with arrays
    resolve(value);
  }
}

Is there any improvement in using promises in this case or am I only overcomplicating this and should just return the value and not a promise?


Solution

  • No, async functions are only syntax sugar on top of promises. If your function doesn't actually have any asynchronous operations (like a network request), it will keep executing synchronously and will resolve synchronously, so it'll be no different from an ordinary function.

    Javascript is single-threaded.