Reflection to know if executed within a generator/async ?

Andrea Giammarchi andrea.giammarchi at gmail.com
Thu Dec 3 16:37:14 UTC 2015


Uhm... you never closed that "will this value be .... thing and I'm
expecting a "dary" answer like in few emails but my whole point is
consuming  an API in different scenarios: held or synchronous.

You can always create an API that always returns a Promise, that's nothing
new for the language, but can you decide if it's he case to absolutely
return a promise 'cause any other value would cause trouble within that
generator or async function?

On Thu, Dec 3, 2015 at 4:15 PM, John Lenz <concavelenz at gmail.com> wrote:

> 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/23add6ab/attachment.html>


More information about the es-discuss mailing list