Reflection to know if executed within a generator/async ?

Andrea Giammarchi andrea.giammarchi at gmail.com
Thu Dec 3 19:04:39 UTC 2015


I guess `held` would be like an arrow function, "transparent" when it comes
to held invokes (like context or arguments)

On Thu, Dec 3, 2015 at 5:23 PM, Claude Pache <claude.pache at gmail.com> wrote:

> How would you detect that the following call to your `fileGetContent`
> function should return a Promise?
>
> ```js
> function oldSchool() {
>     return fileGetContent("foo").then(function (c) {
>         // ....
>     })
> }
> ```
>
> —Claude
>
>
> > Le 3 déc. 2015 à 13:15, Andrea Giammarchi <andrea.giammarchi at gmail.com>
> a écrit :
> >
> > Hi there,
> >   just writing down some thoughts about being able to understand if a
> method/function has been  executed within a generator/async and is being
> yielded/awaited.
> >
> > Rationale: API that would like to behave synchronously in some case,
> returning Promises in other cases.
> >
> > Example:
> >
> > ```js
> > function fileGetContent(fileName) {
> >   // random example
> >   if (held) {
> >     return fetch(fileName).then((r)=>r.text());
> >   } else {
> >     var xhr = new XMLHttpRequest;
> >     xhr.open('GET', fileName, false);
> >     xhr.send(null);
> >     return xhr.responseText;
> >   }
> > }
> > ```
> >
> > Above example will virtually return always the same type and it could
> work inside a generator or an  async function as long as it's being held.
> >
> > Does any of this make sense? Is it worth exploring this pattern?
> >
> > Thanks for any sort of thought.
> >
> > Best  Regards
> >
> >
> >
> >
> > _______________________________________________
> > 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/20151203/e3d1109d/attachment-0001.html>


More information about the es-discuss mailing list