我有一個控制器功能,創建一些東西。當調用該函數時,將運行setInterval
以獲取該項目的狀態。推遲承諾價值不更新/解決/推遲
這裏的服務:
(function() {
'use strict';
function myService($q) {
let deferred = $q.defer();
function createSomething(name) {
Meteor.call('createSomething', name, (err, res) {
if (err) {
deferred.reject(err);
} else {
//value returned is the created item (id, name, status)
deferred.resolve(res);
}
});
return deferred.promise;
}
function getStatus(id) {
Meteor.call('getStatus', id, (err, res) {
if (err) {
deferred.reject(err);
} else {
//statuses are queued, processing, created
deferred.resolve(res);
}
});
return deferred.promise;
}
return {
createSomething: createSomething,
getStatus: getStatus
}
}
angular.module('myApp').factory('myService', myService);
})();
這裏是控制器:
(function() {
'use strict';
function myController($scope, myService) {
let ctrl = this;
ctrl.create = (name) => {
myService.createSomething(name)
.then((item) => {
ctrl.statusInterval = setInterval(() => {
myService.getStatus(item.data.id)
.then((status) => {
//status is always 'queued' :(
if (status.data.status === 'created') {
clearInterval(ctrl.statusInterval);
//do something
}
});
}, 5000);
});
};
}
angular.module('myApp').controller('myController', myController);
})();
當我在服務的getStatus
檢查response
的值,狀態的每一次變化它被稱爲(隊列 - >處理...處理 - >創建)。但是,控制器中的status
的值始終爲queue
。
如何獲得承諾值來解決?
呃,既'createSomething'和'getStatus'試圖解決同樣的延期? – Bergi