我习惯于将$q
与角度 1 一起使用。我正在迁移到角度 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');
}
});
它们应该是等效的