# Promise.delay (sleep) and Promise.timeout

**URL:** <https://es.discourse.group/t/promise-delay-sleep-and-promise-timeout/349>\
**Category:** 💡 Ideas\
**Created:** [June 3, 2020, 4:50pm UTC](https://es.discourse.group/t/promise-delay-sleep-and-promise-timeout/349 "2020-06-03T16:50:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![JackWorks](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jackworks/32/260_2.png) [@JackWorks](https://es.discourse.group/u/JackWorks)\
**Post date:** [June 3, 2020, 4:50pm UTC](https://es.discourse.group/t/promise-delay-sleep-and-promise-timeout/349/1 "2020-06-03T16:50:05Z")

</div>

> Update: ~~please go to [Promise.delay (sleep) and Promise.timeout - APIs - WICG](https://discourse.wicg.io/t/promise-delay-sleep-and-promise-timeout/4644)~~ Please go to [GitHub - Jack-Works/proposal-promise.delay-and-timeout: Promise.delay and Promise.timeout proposal](https://github.com/Jack-Works/proposal-promise.delay-and-timeout)

Related [Sleep function - #11 by velociwabbit](https://es.discourse.group/t/sleep-function/295/11)

```js
await Promise.delay(2000)
// resolves after _at least_ 2000 ms, same as setTimeout
await Promise.timeout(fetchTaskPromise, 2000)
// rejects after 2000ms if fetchTaskPromise not resolved

```

Does anyone interested in this?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [June 3, 2020, 6:11pm UTC](https://es.discourse.group/t/promise-delay-sleep-and-promise-timeout/349/2 "2020-06-03T18:11:26Z")

</div>

setTimeout isn't in the language, and other than Date, and the upcoming Temporal.now, there's no concept of a "clock", by design.

I think it'd be hugely useful, but I'm skeptical it'd be possible to specify.

---

<div class="post-metadata">

**Author:** ![JackWorks](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jackworks/32/260_2.png) [@JackWorks](https://es.discourse.group/u/JackWorks)\
**Post date:** [June 3, 2020, 7:14pm UTC](https://es.discourse.group/t/promise-delay-sleep-and-promise-timeout/349/3 "2020-06-03T19:14:27Z")

</div>

> there's no concept of a "clock", by design.

Hmm yes. But it possible to implement a clock by Promise + Date.now().

> setTimeout isn't in the language

What about, if the host is providing a setTimeout, it should also extends Promise.delay and timeout? So we can refer to the setTimeout and still not specifying them in the spec.

---

<div class="post-metadata">

**Author:** ![JackWorks](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jackworks/32/260_2.png) [@JackWorks](https://es.discourse.group/u/JackWorks)\
**Post date:** [June 3, 2020, 7:27pm UTC](https://es.discourse.group/t/promise-delay-sleep-and-promise-timeout/349/4 "2020-06-03T19:27:13Z")

</div>

Interesting, I try to impl a setTimeout by using only blocks in the ES spec.

```javascript
async function myTimeout(time, callback) {
    const now = Date.now()
    while (true) {
        await Promise.resolve(0)
        if (now + time < Date.now()) return callback()
    }
}

```

And this won't work, it's blocking the main thread. I think I'm not master at event loops micro tasks etc. But is it even possible to do that? 🤔

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [June 3, 2020, 8:56pm UTC](https://es.discourse.group/t/promise-delay-sleep-and-promise-timeout/349/5 "2020-06-03T20:56:30Z")

</div>

I think not without workers or requestAnimationFrame or setTimeout or setInterval, which also all aren't in the language.

---

<div class="post-metadata">

**Author:** ![JackWorks](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jackworks/32/260_2.png) [@JackWorks](https://es.discourse.group/u/JackWorks)\
**Post date:** [June 4, 2020, 1:54pm UTC](https://es.discourse.group/t/promise-delay-sleep-and-promise-timeout/349/6 "2020-06-04T13:54:41Z")

</div>

Possible spec text:

Promise.delay(timeout):

This section is optional for the hosts that doesn't have a builtin "setTimeout" (close to the HTML semantics). If hosts decided to implement this, it's RS should be very close to the semantics in HTML if it is possible so developers won't be surprised.

Algorithm:

1. If Type(_timeout_) is not "number", throw TypeError
2. Let _promise_ be new %Promise%
3. Call(%setTimeout%, %globalThis%, [resolver function of _promise_, _timeout_)
4. return _promise_

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [June 4, 2020, 2:39pm UTC](https://es.discourse.group/t/promise-delay-sleep-and-promise-timeout/349/7 "2020-06-04T14:39:03Z")

</div>

node’s setTimeout returns an object timer.

The only way this could be specified, i think, is by including setTimeout in 262 - we can’t just refer to the HTML spec. “Optional” still means non-web engines including non-node) can choose to implement it, and they shouldn’t have to read the HTML spec to do it.

---

<div class="post-metadata">

**Author:** ![JackWorks](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jackworks/32/260_2.png) [@JackWorks](https://es.discourse.group/u/JackWorks)\
**Post date:** [June 4, 2020, 2:43pm UTC](https://es.discourse.group/t/promise-delay-sleep-and-promise-timeout/349/8 "2020-06-04T14:43:47Z")

</div>

Maybe, we can vaguely describe this function (since there is no clock in the es spec) and give suggested algorithm (not requirement), then suggest hosts to define their own version.

---

<div class="post-metadata">

**Author:** ![kriskowal](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/kriskowal/32/326_2.png) [@kriskowal](https://es.discourse.group/u/kriskowal)\
**Post date:** [June 8, 2020, 2:25pm UTC](https://es.discourse.group/t/promise-delay-sleep-and-promise-timeout/349/9 "2020-06-08T14:25:21Z")

</div>

Perhaps it’s possible to Promise.delay in terms of clock semantics that are a suitable common foundation for describing setTimeout without specifying setTimeout. Promise.delay would also need a cancellation mechanism, which also means possibly hoisting cancellation tokens up.

This seems desirable, possible, and laden with dependencies.

---

<div class="post-metadata">

**Author:** ![JackWorks](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jackworks/32/260_2.png) [@JackWorks](https://es.discourse.group/u/JackWorks)\
**Post date:** [July 11, 2020, 3:15am UTC](https://es.discourse.group/t/promise-delay-sleep-and-promise-timeout/349/10 "2020-07-11T03:15:33Z")

</div>

Moved the discussion to WICG: [https://discourse.wicg.io/t/promise-delay-sleep-and-promise-timeout/4644](https://discourse.wicg.io/t/promise-delay-sleep-and-promise-timeout/4644)

---

<div class="post-metadata">

**Author:** ![JackWorks](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jackworks/32/260_2.png) [@JackWorks](https://es.discourse.group/u/JackWorks)\
**Post date:** [October 2, 2020, 12:07pm UTC](https://es.discourse.group/t/promise-delay-sleep-and-promise-timeout/349/11 "2020-10-02T12:07:23Z")

</div>

> **[Jack-Works/proposal-promise.delay-and-timeout](https://github.com/Jack-Works/proposal-promise.delay-and-timeout)**
>
> Promise.delay and Promise.timeout proposal. Contribute to Jack-Works/proposal-promise.delay-and-timeout development by creating an account on GitHub.
