Consider p and f (variable or expression) where
p.then(f)
is the same as f(await p)
,
as shown in the examples below.
Ex 1: navigator.getBattery()
is a Promise,
display
is a function
is the same as navigator.getBattery().then(display)
display(await navigator.getBattery())
Ex 2: u
is a URL, fetch(u)
is a Promise,
call on the Response r.json()
is a Promise,
ask
is a function
is the same as fetch(u).then(r => r.json()).then(ask)
r=await fetch(u); ask(await r.json())
They are not exactly the same, it is strongly recommended
here
that await
should be prefered. See also
MDN docs and JS info