4 ms·
I'm curious how you would you do something like the following? Is that just not supported? const promise1 = job1(); const promise2 = job2(); const
by paldepind2 2y ago
I'm curious how you would you do something like the following? Is that just not supported?
const promise1 = job1();
const promise2 = job2();
const [result1, result2] = Promise.all(promise1, promise2);
- recursivedoubts 2y agoIf 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.
- paldepind2 2y agoThanks 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? const result = Promise.race(job1(), job2());
- recursivedoubts 2y agoYou 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.
- gpderetta 2y agoYou could imagine an hypothetical language with implicit awaits but explicit forks: const promise1 = async job1(); const promise2 = async job2(); const [result1, result2] = Promise.all(promise1, promise2);