Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #33214 > unrolled thread

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

Started byMartin Honnen <martin.honnen@gmx.de>
First post2017-05-05 13:30 +0200
Last post2017-05-06 01:16 +0200
Articles 3 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  How would using await be possible in an event handler content attribute? Martin Honnen <martin.honnen@gmx.de> - 2017-05-05 13:30 +0200
    Re: How would using await be possible in an event handler content attribute? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 20:02 +0200
      Re: How would using await be possible in an event handler content attribute? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-06 01:16 +0200

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

FromMartin Honnen <martin.honnen@gmx.de>
Date2017-05-05 13:30 +0200
SubjectHow would using await be possible in an event handler content attribute?
Message-ID<oehnpk$i0b$1@news.albasani.net>
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?

[toc] | [next] | [standalone]


#33225

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-05-05 20:02 +0200
Message-ID<1885432.OBFZWjSADL@PointedEars.de>
In reply to#33214
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.

[toc] | [prev] | [next] | [standalone]


#33228

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-05-06 01:16 +0200
Message-ID<4012654.cEBGB3zze1@PointedEars.de>
In reply to#33225
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?

-- 
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web