I created a class that has a "then" method. This class is unrelated to the Promise type; the "then" method has a different purpose and doesn't return a promise. I am trying to write an async/await function in Typescript 2.1.4 that awaits and returns an instance of this class, but the Typescript server in VS Code is giving me errors. If I rename the method to something other than "then", the errors go away.
Example code with errors:
class MyClass {
then(): number {
// this method isn't related to Promise.then
return 2 + 2;
}
}
// three errors below go away when "then" is renamed
// [ts] An async function or method must have a valid awaitable return type.
async function my_async(): Promise<MyClass> {
let a: Promise<MyClass> = Promise.resolve(new MyClass());
// [ts] Operand for 'await' does not have a valid callable 'then' member.
let b: MyClass = await a;
// [ts] Return expression in async function does not have a valid callable 'then' member.
return b;
}
Can someone explain why using promises with an object that has its own "then" method is not allowed, or a work around?
.then
method that isn't from a promise.