相当于 angular 中的 $q.when 2
equivalent of $q.when in angular 2
我习惯用$q
和angular 1.我正在迁移到angular 2.
是否有提供 .when()
方法的等效方法?
例如我需要迁移这个:
.service('updateProDB', [
'$rootScope',
'connectionStatus',
'$q',
'storageService',
'sendToServer',
'$ionicPopup',
function ($rootScope, connectionStatus, $q, storageService, sendToServer, $ionicPopup) {
'use strict';
var dbReadyDeferred = $q.defer(),
prodata = [],
prodataFieldNames = [];
this.get = function () {
var debugOptionUseLocalDB = 0,
prodata = [],
serverAttempts = 0;
if (debugOptionUseLocalDB) {
return fallbackToLocalDBfileOrLocalStorageDB();
}
if (connectionStatus.f() === 'online') {
console.log("Fetching DB from the server:");
return getDBfileXHR(dbUrl(), serverAttempts)
.then(function () { // success
console.log('-normal XHR request succeeded.');
return dbReadyDeferred.promise;
})
.catch(function (){
你可以做同样的事情,用:
Promise.resolve(promise).then
这基本上没有区分承诺和价值。
是的,您可以用相同的方式实例化 q,但使用 new Promise()
而不是 $q()
let promise = new Promise((resolve, reject) => {
if (/* some async task */) {
resolve('Success!');
} else {
reject('Oops... something went wrong');
}
});
和
let promise = $q((resolve, reject) => {
if (/* some async task */) {
resolve('Success!');
} else {
reject('Oops... something went wrong');
}
});
它们应该是等价的
我习惯用$q
和angular 1.我正在迁移到angular 2.
是否有提供 .when()
方法的等效方法?
例如我需要迁移这个:
.service('updateProDB', [
'$rootScope',
'connectionStatus',
'$q',
'storageService',
'sendToServer',
'$ionicPopup',
function ($rootScope, connectionStatus, $q, storageService, sendToServer, $ionicPopup) {
'use strict';
var dbReadyDeferred = $q.defer(),
prodata = [],
prodataFieldNames = [];
this.get = function () {
var debugOptionUseLocalDB = 0,
prodata = [],
serverAttempts = 0;
if (debugOptionUseLocalDB) {
return fallbackToLocalDBfileOrLocalStorageDB();
}
if (connectionStatus.f() === 'online') {
console.log("Fetching DB from the server:");
return getDBfileXHR(dbUrl(), serverAttempts)
.then(function () { // success
console.log('-normal XHR request succeeded.');
return dbReadyDeferred.promise;
})
.catch(function (){
你可以做同样的事情,用:
Promise.resolve(promise).then
这基本上没有区分承诺和价值。
是的,您可以用相同的方式实例化 q,但使用 new Promise()
而不是 $q()
let promise = new Promise((resolve, reject) => {
if (/* some async task */) {
resolve('Success!');
} else {
reject('Oops... something went wrong');
}
});
和
let promise = $q((resolve, reject) => {
if (/* some async task */) {
resolve('Success!');
} else {
reject('Oops... something went wrong');
}
});
它们应该是等价的