admin管理员组

文章数量:1194938

I have 2 functions, both returning promises:

    var getToken = function() {

        var tokenDeferred = $q.defer();         
        socket.on('token', function(token) {
            tokenDeferred.resolve(token);
        });
        //return promise
        return tokenDeferred.promise;
    }

    var getUserId = function() {
        var userIdDeferred = $q.defer();
        userIdDeferred.resolve('someid');           
        return userIdDeferred.promise;
    }

Now I have a list of topics that I would like to update as soon as these two promises get resolved

    var topics = {      
        firstTopic: 'myApp.firstTopic.',  
        secondTopic: 'myApp.secondTopic.',
        thirdTopic: 'myApp.thirdTopic.',
        fourthTopic: 'myApp.fourthTopic.',
    };

Resolved topics should look like this myApp.firstTopic.someid.sometoken

var resolveTopics = function() {
    $q.all([getToken(), getUserId()])
    .then(function(){

        //How can I resolve these topics in here?

    });
}

I have 2 functions, both returning promises:

    var getToken = function() {

        var tokenDeferred = $q.defer();         
        socket.on('token', function(token) {
            tokenDeferred.resolve(token);
        });
        //return promise
        return tokenDeferred.promise;
    }

    var getUserId = function() {
        var userIdDeferred = $q.defer();
        userIdDeferred.resolve('someid');           
        return userIdDeferred.promise;
    }

Now I have a list of topics that I would like to update as soon as these two promises get resolved

    var topics = {      
        firstTopic: 'myApp.firstTopic.',  
        secondTopic: 'myApp.secondTopic.',
        thirdTopic: 'myApp.thirdTopic.',
        fourthTopic: 'myApp.fourthTopic.',
    };

Resolved topics should look like this myApp.firstTopic.someid.sometoken

var resolveTopics = function() {
    $q.all([getToken(), getUserId()])
    .then(function(){

        //How can I resolve these topics in here?

    });
}
Share Improve this question asked Jun 24, 2014 at 21:13 gumenimedagumenimeda 8357 gold badges16 silver badges43 bronze badges 4
  • 1 They will be resolved by the time code before/after your comment is executed. docs.angularjs.org/api/ng/service/$q#all Are you asking how to get the returned data from each? that's also answered in the documentation at the above link. The first aregument will be an array with values that represent what the promises were resolved with. – Kevin B Commented Jun 24, 2014 at 21:16
  • I am sorry I will edit. I am asking how to update topics with values that are returned by getToken() and getUserId() – gumenimeda Commented Jun 24, 2014 at 21:25
  • How is topics related to the data being returned from getToken and getUserId promises? – Kevin B Commented Jun 24, 2014 at 21:27
  • myApp.firstTopic.someid.sometoken – gumenimeda Commented Jun 24, 2014 at 21:28
Add a comment  | 

1 Answer 1

Reset to default 28

$q.all creates a promise that is automatically resolved when all of the promises you pass it are resolved or rejected when any of the promises are rejected.

If you pass it an array like you do then the function to handle a successful resolution will receive an array with each item being the resolution for the promise of the same index, e.g.:

  var resolveTopics = function() {
    $q.all([getToken(), getUserId()])
    .then(function(resolutions){
      var token  = resolutions[0];
      var userId = resolutions[1];
    });
  }

I personally think it is more readable to pass all an object so that you get an object in your handler where the values are the resolutions for the corresponding promise, e.g.:

  var resolveTopics = function() {
    $q.all({token: getToken(), userId: getUserId()})
    .then(function(resolutions){
      var token  = resolutions.token;
      var userId = resolutions.userId;
    });
  }

本文标签: javascriptHow to resolve qallStack Overflow