I had code below from javascript.info async/await
tutorial and I noticed it has used new Promise
next to await
, this is the cut from the code:
await new Promise((resolve, reject) => setTimeout(resolve, 3000));
img.remove();
link to code
so it’s possible to type this code as below:
await setTimeout(()=>{img.remove()}, 3000));
so I’m wondering what’s the necessity to use new Promise
next to await
keyword(while we eliminate .then()
and .catch()
with await
and try...catch
but I’ve seen new Promises
get used alongside the await
)?
p.n: I thought maybe it gets used when we have conditions for rejection and success inside new Promise
but in this example we have no condition and still it’s used.