await setTimeout in async functions
Andrea Giammarchi
andrea.giammarchi at gmail.com
Tue Feb 28 20:14:49 UTC 2017
In the first example, I haven't written this by accident:
> also granting you args are those passed at the invocation time and no
possible mutation capable of affecting `computeResult` could happen later
on?
which is why I've shown both examples, the before `setTimeout(resolve,
1000, computeResult(...args));` and the after ;-)
On Tue, Feb 28, 2017 at 8:02 PM, T.J. Crowder <
tj.crowder at farsightsoftware.com> wrote:
> 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/3ca35f2b/attachment.html>
More information about the es-discuss
mailing list