What's the equivalent of Angular's $q in A

2020-03-08 12:21发布

问题:

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']);

回答1:

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



回答2:

You can use the native es6 Promise. One of the main reason to make new angular is es6 and nearly comming es7.