how to use es6-promises with typescript?

main.ts

import {Promise} from 'es6-promise';
const p: Promise<string> = new Promise (
   (resolve: (str: string)=>void, reject: (str: string)=>void) => {
      const a: string = "hello from Promise";
      resolve(a);
   }
 );
p.then((st) => {
  console.log(st);
});

tsconfig.json

{
    "compilerOptions": {
        "target": "es3",
        "module": "commonjs",
        "declaration": false,
        "noImplicitAny": false,
        "noLib": false
    },
    "filesGlob": [
        "./**/*.ts",
        "!./node_modules/**/*.ts"
    ],
    "files": [
        "./main.ts",
        "./typings/es6-promise/es6-promise.d.ts"
    ]
}

compileandrun.sh

#!/bin/sh
npm install es6-promise
tsd install es6-promise
tsc
node main.js

You don't need to install any library to use promises, async/await and the rest of the new stuff and target es5. just make sure you include a lib version that supports promises, in general I use esnext - you could be more cautious..

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "lib": ["esnext", "dom"], 
    "strict": true, 
    "esModuleInterop": true,
    "sourceMap": true,
    "outDir": "./dist", 
    "rootDir": ".",
  },
  "include": ["src"]
}

and voila, use promises and async await also:

async function f(url:string){
    const response = await fetch(url)
    var data = await decodeOrThrow(await response.arrayBuffer())
}

The following was on v2.1.1+ with the target set to es5

I was able to use Promises with async/await by installing es6-promise and then adding this to the top of the file:

global.Promise = require('es6-promise').Promise;

And this to tsconfig.json

"lib": [ "es2015.promise", "es5" ],

Using the import { Promise } form did not work for me as other libraries were crashing (ex: axios)


I needed to polyfill this in for a different framework (specifically, axios); I didn't need to actually create my own promises, so none of these solutions worked for me. Fortunately, the answer was simple, if well-hidden:

import { polyfill } from 'es6-promise'

polyfill();