[英]angular promise not resolving correctly
I'm working on getting a promise to resolve after a Firebase query. 我正在努力争取在Firebase查询后解决该问题。 Essentially I want to get all of the specified keys from a table and then loop through another table in order to get the artwork I want.
本质上,我想从一个表中获取所有指定的键,然后遍历另一个表以获取所需的图稿。
artistFactory.js artistFactory.js
'use strict';
angular.module('artvoicesApp')
.factory('Artist', function(localStorageService, FIREBASE_URL, $q) {
var artistData = {};
var userKey = localStorageService.get('userKey');
var accountKey = localStorageService.get('accountKey');
var artistRef = FIREBASE_URL.child('v2/artist');
var accountRef = FIREBASE_URL.child('v2/account/' + accountKey);
var userRef = FIREBASE_URL.child('v2/user/' + userKey);
artistData.addArtist = function(artistName) {
var artist = artistRef.push();
accountRef.child('artists/' + artist.key()).set(true);
userRef.child('artists/' + artist.key()).set(true);
artist.set({name: artistName});
artist.child('users/' + userKey).set(true);
artist.child('accounts/' + accountKey).set(true);
};
artistData.getArtistKeys = function() {
var artistKeys = [];
var defer = $q.defer();
accountRef.child('artists').once('value', function(snapshot) {
snapshot.forEach(function(childSnapShot) {
artistKeys.push(childSnapShot.key());
});
defer.resolve(artistKeys);
});
return defer.promise;
};
artistData.getArtists = function(artistKeys) {
var artistObj = {};
var artistRef = FIREBASE_URL.child('v2/artist');
var defer = $q.defer();
artistKeys.forEach(function(artist) {
artistRef.child(artist).once('value', function(snapshot) {
artistObj[artist] = snapshot.val();
});
defer.resolve(artistObj);
});
return defer.promise;
};
return artistData;
});
artwork.controller.js artwork.controller.js
Artist.getArtistKeys().then(function(artistKeys) {
Artist.getArtists(artistKeys).then(function(artists) {
vm.artists = artists;
console.log(vm.artists);
});
});
If I set vm.artwork to a timeout, it returns the appropriate data. 如果将vm.artwork设置为超时,它将返回适当的数据。
Here's your problem: 这是您的问题:
artistKeys.forEach(function(artist) {
artistRef.child(artist).once('value', function(snapshot) {
artistObj[artist] = snapshot.val(); // <<== This is called second, at an unspecified time in the future
});
defer.resolve(artistObj); // <<== This is called first
});
All of your assignments to artistObj
will occur at some time after you called defer.resolve(artistObj)
. 在您调用
defer.resolve(artistObj)
之后的某个时间,您对artistObj
所有分配都会发生。 This is why it appeared to work once you added a timeout. 这就是为什么添加超时后它似乎可以工作的原因。
You will need to map your collection of artists to a collection of promises, then wait for all of these promises to resolve. 您将需要将您的艺术家收藏映射到承诺的收藏,然后等待所有这些承诺解决。
artistData.getArtistKeys = function(artistKeys) {
var artistObj = {};
var artistRef = FIREBASE_URL.child('v2/artist');
var allPromises = artistKeys.map(function(artist) {
var childDefer = $q.defer();
artistRef.child(artist).once('value', function(snapshot) {
artistObj[artist] = snapshot.val();
childDefer.resolve();
});
return childDefer.promise();
});
// allPromises is now an array of promises
var defer = $q.defer();
$q.all(allPromises).then(function() {
defer.resolve(artistObj);
});
return defer.promise();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.