Reflection to know if executed within a generator/async ?

John Lenz concavelenz at gmail.com
Thu Dec 3 16:15:02 UTC 2015


I don't think being executed in a generator or async function is a good
signal that a promise should be returned (something needs to "await" the
promise).   The async function is really a chain of synchronous code split
at various points, during the synchronous parts the code would still need
to be synchronous and returning a promise would be wrong.   The real data
you need is "will this value be consumed by an 'await' expression or be
used to resolve a Promise and, in the general case,  that is not something
that can't be answered at the time your example function is executing.

On Thu, Dec 3, 2015 at 4:15 AM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> 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/192857d5/attachment.html>


More information about the es-discuss mailing list