I'm using Typescript with es2017 as the compilation target, using Javascript's new async
/ await
.
I currently have the following code to fetch some data from a TS service class (simplified):
class MyClass {
public static async initialize(): Promise<void> {
const data = await this.getItems();
// do unrelated initialization stuff
return new Promise<void>(() => {});
}
private static async getItems(): Promise<Item[]> {
return await Service.fetchData();
}
}
class Service {
public static async fetchData(): Promise<Item[]> {
// Performs an XHR and returns a Promise.
}
}
This works, but it would be a lot cleaner if MyClass::initialize()
did not return anything, rather than returning new Promise<void>(() => {});
However, this seems to be impossible, as any method/function that uses await
has to be marked async
, and any method/function that is marked as async
has to return a promise
.
Is there any way around this, or is there something I'm fundamentally not grasping?