There are two functions that produce the same outcome:
const p1 = async () => {
return 1;
};
const p3 = new Promise((resolve, reject) => {
resolve(1);
});
console.log(typeof p1.then);
console.log(typeof p3.then);
It is surprising that both functions do not transpile to have a "then" property:
https://i.sstatic.net/cfIHX.png
However, vs-code's intellisense considers them both as promises:
https://i.sstatic.net/YUKIo.png
https://i.sstatic.net/x4wyL.png
We can even ensure that the return type is the same if we're explicit about p3:
https://i.sstatic.net/FcKy6.png
Now, a Promise is "thenable" by definition, right? This becomes important as I have an inferred interface in Typescript from the following function:
export const DELAYED = "WAIT_IN_PARALLEL_DELAYED";
export function delayed<T = any>(delayedPromise: () => Promise<T>) {
return {
delayed: DELAYED,
start(resolve, reject) {
delayedPromise()
.then(resolve)
.catch(reject);
}
};
}
I expected that passing in:
const test = () => async() => 1;
to the function delayed(test)
would be acceptable. However, it is indicating that "test" does NOT have a ".then" property. Can someone help me pinpoint the issue in my logic?