What's the equivalent of Angular's $q in Angular2?

David picture David · Jun 9, 2016 · Viewed 17.3k times · Source

What's the equivalent of Angular's $q in Angular2? Specifically, I'm looking for $q.when, which allowed you to do something like:

return $q.when(['TestResponse']);

Answer

Günter Zöchbauer picture Günter Zöchbauer · Jun 9, 2016
new Promise((resolve, reject) => { 
  if(xxx) {
    resolve('ok');
  } else {
    reject('error');
  }
}).then(x => doSomething())

See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise or http://learnangular2.com/es6/promises