Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How would using await be possible in an event handler content attribute?

3 views
Skip to first unread message

Martin Honnen

unread,
May 5, 2017, 7:31:05 AM5/5/17
to

As far as I understand, to use the "await" operator
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
I need to declare the function containing it as "async" e.g.

async function foo(url) {
var response = await fetch(url);
var text = await response.text();
document.getElementById('input1').value = text;
}

I wonder whether there is any way inside HTML to specify that an event
handler content attribute is not a function body but an "async" function
body so that it is possible to use "await" inside the event handler
content attribute.

If I simply use

<input type="button" value="test"
onclick="var response = await fetch(location.href);">

I get an error "Unexpected identifier".

Is there any way to designate an event handler content attribute as an
async function body?

Thomas 'PointedEars' Lahn

unread,
May 5, 2017, 2:02:27 PM5/5/17
to
Martin Honnen wrote:

> As far as I understand, to use the "await" operator
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
> I need to declare the function containing it as "async" e.g.
>
> async function foo(url) {
> var response = await fetch(url);
> var text = await response.text();
> document.getElementById('input1').value = text;
> }

Boy, are they tweaking the language these days.

> I wonder whether there is any way inside HTML to specify that an event
> handler content attribute is not a function body but an "async" function
> body so that it is possible to use "await" inside the event handler
> content attribute.
>
> If I simply use
>
> <input type="button" value="test"
> onclick="var response = await fetch(location.href);">
>
> I get an error "Unexpected identifier".
>
> Is there any way to designate an event handler content attribute as an
> async function body?

I do not know of any. The obvious solution might be

onclick="(async function () {
var response = await fetch(location.href);
}())"

or

onclick="async function blub () {
var response = await fetch(location.href);
}; blub();"

--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

unread,
May 5, 2017, 7:16:35 PM5/5/17
to
Thomas 'PointedEars' Lahn wrote:

> Martin Honnen wrote:
>> If I simply use
>>
>> <input type="button" value="test"
>> onclick="var response = await fetch(location.href);">
>>
>> I get an error "Unexpected identifier".

Me too. To be precise, a SyntaxError exception with that message is thrown
and not caught.

>> Is there any way to designate an event handler content attribute as an
>> async function body?
>
> I do not know of any. The obvious solution might be
>
> onclick="(async function () {
> var response = await fetch(location.href);
> }())"
>
> or
>
> onclick="async function blub () {
> var response = await fetch(location.href);
> }; blub();"

Both work in

| Chromium 57.0.2987.98 (Developer Build) Built on 8.7, running on Debian stretch/sid (64-bit)
| Revision a6a06b78087c9fdb4b12fe0ac1b87fdc10179f8b
| OS Linux
| JavaScript V8 5.7.492.63
| […]
| User Agent Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36
| Command Line /usr/lib/chromium/chromium --disk-cache-size=44040192 --show-component-extension-options --ppapi-flash-path=/usr/lib/pepperflashplugin
nonfree/libpepflashplayer.so --ppapi-flash-version=23.0.0.207 --flag-switches-begin --ignore-gpu-blacklist --flag-switches-end

(<chrome://version>)

So if I had to use one of them, I would use the first one (the AFE).

However, why would one want to produce spaghetti code?
0 new messages