4

I just going through the angular 4 tutorials and have problem with Promises:

My service:

import { Injectable } from '@angular/core';

@Injectable()
export class ContentService {

  constructor() { }

  getProviders() : Promise<any>
  {
    return Promise.resolve(["a","b","c"]);
  }

  getProviderByName(name : string) : Array<String>
  {
    return ["a","b","c", name];
  }
}

Ng cant find the "Promise" since there is no import for it. But in the tutorial and several examples I cant find any imports for that.

What did I miss?? How can I use this Promises in Ng 4?

EDIT: Here are my project configs. Thank you all. Iam sure I can get it fixed now. But I still wonder how I ran into this. Since this is a 1 month old npm installation..

tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

package.json:

"private": true,
  "dependencies": {
    "@angular/animations": "^4.0.0",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "bootstrap": "^3.3.7",
    "core-js": "^2.4.1",
    "font-awesome": "^4.7.0",
    "jquery": "^3.2.1",
    "rxjs": "^5.4.1",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/cli": "1.2.5",
    "@angular/compiler-cli": "^4.0.0",
    "@angular/language-service": "^4.0.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.0.4",
    "tslint": "~5.3.2",
    "typescript": "~2.3.3"
  }
Gregor Sklorz
  • 1,645
  • 2
  • 19
  • 27
  • Promise is global. If you have errors, this is because the project was configured incorrectly. Please, specify project details - TypeScript, Webpack configuration, etc. – Estus Flask Aug 25 '17 at 13:41

1 Answers1

3

Promises aren't a lib, they're a native type, so you don't import them. You need to add Promises to your libs:

tsconfig.json:

{
  "compilerOptions": {
    "lib": ["es2015", "dom"]
  }
}

Remember to check you do have a Promise polyfill (like core-js), or only need to support browsers that already support Promises.

Camille Wintz
  • 951
  • 7
  • 8