I'm wondering if there is any semantic difference between the 2 functions below in ES2017 or later:
async function returnBool() {
const promise = new Promise(((res, rej) => {
setTimeout(() => {
res(true);
}, 1000);
}));
// returns a boolean
return await promise;
}
async function returnPromise() {
const promise = new Promise(((res, rej) => {
setTimeout(() => {
res(true);
}, 1000);
}));
// returns a boolean wrapped in a promise
return promise;
}
I tested both versions on Node and they appear to behave the same. However, I'm looking for some assurances that both versions are syntactically and semantically correct.
In languages such as C#, there is always a discrepancy between the declared return type (Task<TResult>
) and the actual return type in the body (TResult
) in an async
method. Judging from the example above, it seems to be not the case in Javascript? Could this suggest a lack of rigor with JS? What if I want to return a Promise<Promise<T>>
from an async method? (unlikely, but still)
It would be really great if anyone could point to some docs or language standard to clarify this. Thanks!