Reflection to know if executed within a generator/async ?

Raul-Sebastian Mihăilă raul.mihaila at gmail.com
Sat Dec 5 08:40:54 UTC 2015


To Andrea:

A function like the one you describe would be practical only if, when
called in a sync context, it could wait by blocking, unlike when being
called in an async context. Otherwise, it doesn't work:

```
let theValue;

function fetchTheValue() {
    return fetch('/theValue')
        .then(r => r.text())
        .then(v => theValue = v);
}

function getTheValue() {
    if (theValue == null) {
        if (held) { return fetchTheValue(); }

        !!! return what?
    }

    if (held) {
        return Promise.resolve(theValue);
    }

    return theValue;
}
```

This example shows that if you don't have the value, but call the function
from an async context, you will get the value. But if you call it from a
sync context you don't get the value. It means that without
waiting-by-blocking, your function behaves differently depending on the
context in which it's called.

Waiting-by-blocking is usually a bad thing and I believe it shouldn't be in
the language (it can be provided by the host environment).

Also I don't understand how the yielding context would be the same as the
await context, since you can yield synchronously. If I write:

```
yield getTheValue();
```

why should held be true?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151205/d8c6cf15/attachment.html>


More information about the es-discuss mailing list