Downlevel Async Functions

This feature was supported before TypeScript 2.1, but only when targeting ES6/ES2015.TypeScript 2.1 brings the capability to ES3 and ES5 run-times, meaning you’ll be free to take advantage of it no matter what environment you’re using.

Note: first, we need to make sure our run-time has an ECMAScript-compliant Promise available globally.That might involve grabbing a polyfill for Promise, or relying on one that you might have in the run-time that you’re targeting.We also need to make sure that TypeScript knows Promise exists by setting your lib flag to something like "dom", "es2015" or "dom", "es2015.promise", "es5"

Example
tsconfig.json
  1. {
  2. "compilerOptions": {
  3. "lib": ["dom", "es2015.promise", "es5"]
  4. }
  5. }
dramaticWelcome.ts
  1. function delay(milliseconds: number) {
  2. return new Promise<void>(resolve => {
  3. setTimeout(resolve, milliseconds);
  4. });
  5. }
  6. async function dramaticWelcome() {
  7. console.log("Hello");
  8. for (let i = 0; i < 3; i++) {
  9. await delay(500);
  10. console.log(".");
  11. }
  12. console.log("World!");
  13. }
  14. dramaticWelcome();

Compiling and running the output should result in the correct behavior on an ES3/ES5 engine.