I'd leave the syntax up to the official body, I'm not sure how these are usually defined but I think the f# syntax would work so I wouldn't mind re-using them.
The advantage of CE's is that it's more abstract that async/await and thus more powerful and allowing userland to define a bunch of these, so for promises there is almost no extra benefit to this than pure async/await (await could be repaced with let!
/ we could use a similar keyword as await instead of let!) but more on that later.
For iteration there also might not be a benefit, I'd have to type it out more clear.
For null-ability there would be a benefit, imagine this piece of code:
function foobar() {
const x = someFunction()
if (x == null) {
return null
}
myFunction(x)
}
This cannot be rewritten using ?.
or ??=
, however, you could build an option
CE that could work such that the following snippet is equivalent to the above (notice the option function
and const!
option function foobar() {
const! x = someFunction()
myFunction(x)
}
But nullability is not the only place where you could use this. E.g. Promises are actually doing 2 things: they return later (the async stuff) and they can be successfull / fail (the "result" stuff), if we were to have something like this we could build a CE that only does the latter so (expected) errors can be returned as a value instead of having to be thrown (similar to callback-style where errors were passed instead of thrown), while still allowing early return using CE so you don't end up with go-like code with a bunch of boilerplate to return an error if another function returned an error
const [value, error] = myFunction()
if (error) {
return error
}
const [otherValue, otherError] = myOtherFunction()
if (otherError) {
return otherError
}
Another benefit would be that you could use these CE's in "value position", so you could do something like
function myFunction () {
const myPromise = async {
await new Promise(resolve => setTimeout(resolve, 1000))
}
}
which would allow to use await in a async function OR an async block, so you don't have to use an async immediately invoking function / extract an async function if you want to use the await syntax for something but can use an inline async block