相当于 $q.when 在角度 2 [英] equivalent of $q.when in angular 2
本文介绍了相当于 $q.when 在角度 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我习惯于将 $q
与 angular 1 一起使用.我正在迁移到 angular 2.
I am used to using $q
with angular 1. I am migrating to angular 2.
是否有提供 .when()
方法的等价物?
Is there an equivalent that provide a .when()
method ?
例如我需要迁移这个:
.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 (){
推荐答案
你可以做同样的事情,用:
You can do the equivalent, with:
Promise.resolve(promise).then
这基本上没有区分承诺和价值.
Which basically makes no distinction between promises and values.
是的,你可以用同样的方式实例化 q,但是使用 new Promise()
而不是 $q()
And yes you can instantiate q the same way, but with new Promise()
rather than $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.when 在角度 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文