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

2

25

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']);
Picked answered 9/6, 2016 at 7:20 Comment(3)
EcmaScript 6 PromiseD
Does the native promise have a when equivalent?Picked
Promise.resolve(['TestResponse']) should do the trickWedged
D
21
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

Dys answered 9/6, 2016 at 7:22 Comment(4)
What if we're stuck at using ES5? What should we do?Monition
IMHO stay with Angular1.xProline
That's of course an option. But in slow migration scenarios a solution can help.Monition
Actually I don't know about ES5 with Angular2.Proline
G
1

You can use the native ES6 Promise. One of the main reasons to make the new angular is ES6 and the upcoming ES7.

Georgettageorgette answered 9/6, 2016 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.