A promise that resolves after a delay.

Bob Myers rtm at gol.com
Thu Feb 4 04:39:16 UTC 2016


Easy to write yourself:

```js
function wait(ms) {
  return function(v) {
    return new Promise(resolve => setTimeout(() => resolve(v), ms));
  };
}
```

Now you can do

```
Promise.resolve(42) . then(wait(1000)) . then( /* cb */);
```

and the 42 will get passed through the callback.

--Bob

On Thu, Feb 4, 2016 at 2:21 AM, JD Isaacks <jd at jisaacks.com> wrote:

> I think it would be super useful to be able to do something like:
>
> Promise.after(1000).then( /* cb */ );
>
> and have the promise resolve after 1000 milliseconds.
>
> The promise would just resolve with the same value (in this case 1000) but
> could easily delay resolving another value like so:
>
> Promise.after(1000).then(() => "other value");
>
> instead of currently having to use a setTimeout:
>
> new Promise(resolve => setTimeout(() => resolve("some value"), 1000));
>
> I believe Promise.after would be so much more readable and useful.
>
> I am not sure if the name `after` is the best name, `wait`, `when`,
> `sleep` are possible names or maybe there is a better name I haven't even
> thought of.
>
> _______________________________________________
> 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/20160204/22d656fd/attachment.html>


More information about the es-discuss mailing list