await setTimeout in async functions
T.J. Crowder
tj.crowder at farsightsoftware.com
Tue Feb 28 20:02:00 UTC 2017
On Tue, Feb 28, 2017 at 7:47 PM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:
>
> Why not using fill setTimeout API, also granting you args are those passed
> at the invocation time and no possible mutation capable of affecting
> `computeResult` could happen later on?
>
>
> ```js
>
> const asyncFunc = (...args) => new Promise((resolve) => {
>
> setTimeout(resolve, 1000, computeResult(...args));
>
> });
>
> ```
That would call `computeReult` *before* the timeout fired, whereas
apparently it should be after the delay.
Your second one, though, is nice and simple. Using Jérémy's `delay`, it
looks like this:
```js
const asyncFunc = (...args) =>
delay(1000).then(() => computeResult(...args));
```
-- T.J. Crowder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170228/dbe239d2/attachment.html>
More information about the es-discuss
mailing list