how to use es6-promises with typescript?

I read this SO question but having trouble getting promises to work with typescript. Hopefully we can make a clear guide. This is for a server/node project. I'm actually using latest iojs, but targeting ES5 as output.

$ tsd query es6-promise --action install --save
$ npm install --save es6-promise


// typescript code:

/// <reference path="../../typings/es6-promise/es6-promise.d.ts"/>

var Promise = require("es6-promise").Promise;
require('es6-promise').polyfill();

function test():Promise {
    var p:Promise = new Promise();
    return p;
}

this is giving the error:

Cannot find name 'Promise'.

// alternatively:

var p = new Promise<string>((resolve, reject) => {
    resolve('a string');
});


//error=> Untyped function calls may not accept type arguments.

What is the recommended way to return a Promise from your own node server side code?

references:

  • https://www.npmjs.com/package/es6-promise

  • https://github.com/borisyankov/DefinitelyTyped/blob/master/es6-promise/es6-promise-commonjs-tests.ts


Solution 1:

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

Solution 2:

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)

Solution 3:

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();

Solution 4:

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())
}

Solution 5:

Add the following to package.json:

"dependencies": {
  "es6-promise": "~4.1.0"
},
"devDependencies": {
  "@types/es6-promise": "^0.0.32"
}