50

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:

6 Answers 6

52

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
7
  • 2
    Is it now recommended to use typings rather than tsd ? Mar 8, 2016 at 9:58
  • 5
    Note for those using Angular 2: you don't need (and shouldn't use) the import {Promise} statement, not do you need es6-promise.d.ts, as this comes bundled with the Angular 2 framework (as of beta 15, anyway). Apr 19, 2016 at 23:07
  • is 'es6-promise' something that you have to download?
    – Daryl
    Aug 24, 2016 at 13:15
  • 3
    If you use Typescript 2+, you can specify the promise typing in tsconfig.json: "compilerOptions": { "lib": [ "es2015.promise", (your other libs, ex "es5" and "dom") ] }
    – skovmand
    Aug 31, 2016 at 7:40
  • 2
    typings install --save --global dt~es6-promise was enough with tsc --version Version 2.0.3
    – devsmt
    Oct 26, 2016 at 8:46
15

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)

9

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();
1
  • 1
    Super belated response, but I just wanted to chime-in that yours was personally my go-to approach. TS 2.7.2 with no other framework (e.g., no Angular), the es6 promise and then polyfill() was the easiest way to support IE11. Thanks.
    – mschofield
    Mar 5, 2018 at 19:33
1

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())
}
1
  • this question was way back in the day when io.js was a thing... seems to all work out of the box now, like you say!
    – dcsan
    Sep 13, 2019 at 22:32
1

Add the following to package.json:

"dependencies": {
  "es6-promise": "~4.1.0"
},
"devDependencies": {
  "@types/es6-promise": "^0.0.32"
}
2
  • 1
    If I do this, do I need to import Promise or can I just use it? Oct 18, 2017 at 11:06
  • 1
    @MuhammadRehanSaeed If you do this you have to import {Promise,} from 'es6-promise'; to use it. That’s what makes this “solution” ugly…
    – binki
    Mar 30, 2018 at 22:36
-3

Change target to "es6" in your tsconfig.json

"compilerOptions": {"target": "es6" }

Or install TypeScript for Visual Studio 2015 can also solve this problem without modify tsconfig.json

https://www.microsoft.com/en-us/download/details.aspx?id=48593

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.