Promise-returning delay function

James M Snell jasnell at gmail.com
Sun Dec 27 23:01:33 UTC 2020


For Node.js we implemented this as an alternative to the current setTimeout:

const { setTimeout } = require('timers/promises')

// Or

import { setTimeout } from 'timers/promises'

Then...

await setTimeout(500)

It works very well and is straightforward to implement on the browser-side.

On Sun, Dec 27, 2020, 09:56 Cyril Auburtin <cyril.auburtin at gmail.com> wrote:

> Follow up on https://esdiscuss.org/topic/promise-returning-delay-function
>
> I think we really need that function, with AbortController support
>
> We could do things like
>
> ```js
> const abortFetch = new AbortController();
> const abortTimeout = new AbortController();
>
> await Promise.race([
>   Promise.delay(5000, {signal: abortTimeout.signal}),
>   fetch(url, {signal: abortFetch.signal}).then(r => r.json()).then(...),
> ])
>   .finally(() => {
>     abortFetch.abort();
>     abortTimeout.abort(); // avoid mem leaks, that would call clearTimeout
> under the hood
>   })
> ```
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20201227/c2606fff/attachment.html>


More information about the es-discuss mailing list