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
TypeScript supports asynchronous functions for machines that have native ES6 generators. In this way, TypeScript allows the usage of promises that are from native ES6. This will include the compatibility of native ES6 promises without setting the target to ES6. While using the above code, the Promise should exist in the runtime for sure.
TypeScript promise holds the future value either it will return success or gets rejected. You can see in the example below, how to define Promise, declare Promise with new operator instance and pass resolve and reject parameter within the inner function in the Promise object. A promise accepts callback function as a parameter.
We'll start by looking at ES6 Promises, before heading on over to details about Fetch What is a Promise? Much like in the real world, a promise is the result of saying we will do something and give something back. Let's say we wanted to run this piece of code:
For that, a simple promise is created below. Here, the promise is said to have the generic type of strings and basically performs two operations, resolve or reject. Our promise can be resolved or rejected which is carried out by the following piece of code which is known to be the callback.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With