Home > Back-end >  How to create an async function that never resolves?
How to create an async function that never resolves?

Time:10-15

It's easy to create a Promise that never resolves, by passing a function that does not call the resolve callback:

const p = new Promise(() => {})

How can I create an async function that does the same?

async function neverResolve() {
  // ???
}

CodePudding user response:

Either explicitly return such a Promise from it

async function neverResolve() {
  return new Promise(() => {});
}

neverResolve()
  .then(console.log)
  .catch(console.log)

Or await one.

async function neverResolve() {
  await new Promise(() => {});
}

neverResolve()
  .then(console.log)
  .catch(console.log)

  • Related