0

What happens if a promise is never resolved or rejected? For example if I were to write a function that returned a promise but it never resolved or rejected it and put that on a loop. What happens? Would it slow things down eventually? does it just reject automatically if its taking too long?

  • 1
    Answered here : https://stackoverflow.com/questions/57412416/awaited-but-never-resolved-rejected-promise-memory-usage – Eldar Nov 11 '19 at 19:33

1 Answers1

0

No, it will not be rejected and you need to use some race condition using timeout. e.g.

const sleep = seconds => new Promise(res => setTimeout(res, seconds * 1000));
const dataPromise = db.getUser();
return Promise.race([dataPromise, sleep(10)]);

This is how either you will get data or nothing. You need to add your logic to handle that.

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60