简体   繁体   中英

angular promise not resolving correctly

I'm working on getting a promise to resolve after a Firebase query. 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

'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

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.

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) . 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM