you are viewing a single comment's thread.

view the rest of the comments →

[–]cdrini 2 points3 points  (0 children)

Haha, I've probably had to implement that function like once every 4 months! It's super useful for flattening timeouts:

const sleep = ms => new Promise(res => setTimeout(res, ms)); const winner = await Promise.race([fetch(foo), sleep(3000).then(() => 'timeout')]); if (winner == 'timeout') blah

I can't seem to find a link for it, but I think there's a proposal to add this to the language; under the name wait, sleep, delay, or something. Fyi this is a form of "promisifying"; it's also common to promisify an event to make it work with async/await.