1

I'm trying to use a Promise in my TypeScript application. But it doesn't seem like it's possible?

First I tried to do use Promise.all(), but then I of course get "'Promise' only refers to a type, but is being used as a value here". And it doesn't make a difference if I instead try to instantiate the Promise by doing new Promise().all() (or doing that in two steps, i.e first instantiating and then calling the method). And it also gives the same error message in my function headers where I do async getData(): Promise<X>, which doesn't make any sense to me.

As suggested in some other threads about this issue I changed the target to "es6" from "es5" in my tsconfig.json. I also tried changing it to "es2005". This allowed me to build the application. But when I try to run it I get

Failed to instantiate module [module name] due to: Error: [$injector:nomod] Module '[module name]' is not available

Is there perhaps something else I also need to adjust when changing from es5 to es6?

I also tried adding "lib": [ "es2015" ] to the compilerOptions in tsconfig.json as sugggested in another thread. But then I get a few hundred errors saying stuff like: Build: Cannot find name 'Element'. File: angular.d.ts

Edit: As suggested in the comments I also added "dom" to the lib array, which got rid of the compilation errors. But then instead get "ReferenceError: 'Promise' is undefined".

How do I solve this? I'm fine with any solution that lets me use Promise.

Thanks in advance!

Jonatan Stenbacka
  • 1,824
  • 2
  • 23
  • 48
  • Possible duplicate of [How to use Typescript with native ES6 Promises](https://stackoverflow.com/questions/27573365/how-to-use-typescript-with-native-es6-promises) – Pritilender Apr 15 '19 at 15:13
  • When you include `lib` you will need a bunch of things. Specifically to include "Element" also include "dom" in the lib array. In editors like Visual Studio Code there should be suggestions for these. You're just telling it to load specific .d.ts files from the node_modules by doing this. – Catalyst Apr 15 '19 at 15:50
  • When I also added "dom" to the lib array I get rid of the compiling errors, but then I get "ReferenceError: 'Promise' is undefined" on my function head `async getData(records): Promise` (I assume) when I run the application. – Jonatan Stenbacka Apr 16 '19 at 07:09
  • If you target ES5, you might have to explicitly import `Promise` from a library (polyfill) yourself. – Bergi Apr 16 '19 at 07:16
  • Are you using angular 1? I don't recommend to use native promises there, native promises will not trigger an angular digest loop for change detection, you will have to add those by hand everywhere – Tamas Hegedus Apr 16 '19 at 07:19
  • AngularJS 1.4.9. Not sure exactly what you're saying, but what is the alternative if I need to execute services in parallel and wait for the result? – Jonatan Stenbacka Apr 16 '19 at 07:22
  • I solved it by adding "dom" to the lib array as well as importing Promise to my project by adding ` ` – Jonatan Stenbacka Apr 16 '19 at 09:08

0 Answers0