My web scripting language, https://hyperscript.org, tries to hide the difference between sync and async by resolving promises in the runtime. My theory is that this is something that web script writers should not be concerned with (this theory makes more sense when you consider hyperscript is a companion to htmx, and favors a particular approach to scripting[1].)
on click
fetch /whatever as json
put the result's data into #some-div
wait 2s
put '' into #some-div
You don't have to mark the script as sync or async, and the runtime will resolve everything for you, making everything feel like synchronous scripting.
This obviously has limitations and foot guns, but it works reasonably well for light scripting. More info here:
If you want job1 and job2 to be executed in parallel, you could do this:
set result to [job1(), job2()]
set result1 to the first result
set result2 to the last result
The array expression will wait until all promise values resolve.
This isn't really what hyperscript is designed for, however: its async behavior is designed instead to take the async/sync distinction (and callback hell) out of "normal" DOM scripting. If you have sophisticated async needs then JavaScript is probably a better option. The good news is that hyperscript can call JavaScript functionality in the normal way, so that is an easy option if hyperscripts behavior isn't sufficient for your needs.
Thanks for the reply. I can understand that the purpose here isn't to be as powerful as JavaScript, but rather to be simpler and easier. That being said, is there a way to achieve the below or is that beyond the limits?
You can't achieve that with the intended semantics directly in hyperscript, because it will resolve all argument promises before invoking Promise.race()
You could move that out to JavaScript and then call that function from hyperscript:
set result to jsPromiseRace()
it could also be done as inline js:
js
return Promise.race(job1(), job2());
end
log the result
but I think if you are writing this style of code it makes more sense to just kick out to JavaScript for it.
This obviously has limitations and foot guns, but it works reasonably well for light scripting. More info here:
https://hyperscript.org/docs/#async
And the implementation of it in the runtime here:
https://github.com/bigskysoftware/_hyperscript/blob/c81b07ce...
https://github.com/bigskysoftware/_hyperscript/blob/c81b07ce...
--
[1] - https://htmx.org/essays/hypermedia-friendly-scripting/