Woww
Woww

Reputation: 374

Create promise which never fulfils

How can I create a promise which will never fulfil? So that for example this will never reach console.log:

const res = await NEVER_FULLFILLING_PROMISE

console.log(res)

Upvotes: 0

Views: 62

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370879

Just call new Promise and never call the first parameter in the callback.

(async () => {
  await new Promise(() => {});
  console.log('done');
})();

Another approach that fulfills the text of your question (but probably not the intent) would be to make the Promise reject, of course.

(async () => {
  await new Promise((_, reject) => reject());
  console.log('done');
})()
  .catch(() => {});

Upvotes: 2

Related Questions