Sync Await
I want to be able to code that can optionally use an asynchronous database or a synchronous in-memory database. The best option for doing this would be coroutines / generators. However, Typescript doesn't have good type support for generators. Even TypeScript 3.6 doesn't let your associate the left and right side of a yield
. Another related concept here is algebraic datatypes.
Long story short, I've created a hack where you can use async/await in TypeScript but have await
evaluate synchronously if the Promise resolves synchronously.
Usage
// __awaiter and __generator need to be defined at the top of the file. These will// get picked up by the async/await polyfill so long as your tsconfig.json outputs es5. // Suppose there is a sync way to do things and an async way to do things. // Some business logic that can use either sync or async api.