When using the JavaScript async function (or a regular function that returns a Promise), any object with a function containing a "then" field is considered a Promise. However, is it possible to have such an object be the resolved value of a Promise?
For example, when calling the function await f()
, no result is returned. If the field name in then
is different, then a result is actually returned.
async function f() {
return {
then() {
//This function simply has a fitting name as "then".
return 42;
}
};
}
async function main() {
let result = await f(); // execution stops here.
console.log(result);
console.log(result.then());
}
main();