简体   繁体   English

用于循环承诺的本机/ $ q Javascript

[英]Native/$q Javascript For Loop Promise Resolve

I think I'm getting closer to understanding Javascript Promises, but there's a new issue with for loops. 我认为我越来越接近理解Javascript Promises,但是for循环存在一个新问题。 I've got a function that feeds another Spotify URIs, for the second method to retrieve track information and then return to be included in an array. 我有一个提供另一个Spotify URI的函数,用于第二种方法来检索轨道信息,然后返回包含在数组中。 So far I've been finding solutions that use the Bluebird library, but when using AngularJS, integration isn't quite as simple that I can tell. 到目前为止,我一直在寻找使用Bluebird库的解决方案,但是当使用AngularJS时,我知道集成并不是那么简单。

I'm basically getting back a collection of promises that are not resolved to data. 我基本上是拿回一些尚未解决数据的承诺。

This is the function in my controller, feeding data to the second function in services: 这是我控制器中的函数,将数据馈送到服务中的第二个函数:

$scope.getUserRecent = function () {
    var id = window.localStorage.getItem('id');
    return UserService.getTracks(id).then(function (data) {
        var tracks = {}, i;

        console.log(data);

        for (i = 0; i < data.length; i++) {
            tracks[i] = SpotifyService.getTrack(i, data[i].uri, data[i].loved, data[i].from);
            console.log(i + ": " + tracks[i]);
        }
        console.log(tracks);

        return tracks;
    })
    .catch(function (error){
        console.log(error);
    });
};

The function on the other side works absolutely fine, pulling the data down and returning it as it should; 另一边的功能绝对正常,可以拉下数据并按需返回。 the promises, however, are not resolved. 但是,诺言没有兑现。 How can I fix this? 我怎样才能解决这个问题?

Edit: Access to $scope.getUserRecent() : 编辑:访问$scope.getUserRecent()

$scope.refresh = function () {
    $q.all($scope.getUserRecent()).then(function (data) {
        console.log(data);
        $scope.tracks = data;
    })
    .catch(function (error) {
        console.log(error);
    });

    $scope.$broadcast('scroll.refreshComplete');
};

$scope.$on('$ionicView.enter', function (e) {
    $scope.refresh();
});

UserService.getTracks(id) returns: UserService.getTracks(id)返回:

[Object, Object]
    0:Object
        from:"string"
        id:1
        loved:true
        sharerId:1
        uri:"6mNMp0g3zkg1K7uBpn07zl"
        __proto__:Object
    1:Object
        from:"string"
        id:2
        loved:true
        sharerId:1
        uri:"14WWzenpaEgQZlqPq2nk4v"
        __proto__:Object
    length:2
    __proto__:Array[0]

In order to use a consumer function $scope.refresh as is you should resolve the promises, then return a new (different) one : 为了按$scope.refresh使用使用者函数$scope.refresh ,您应该解析promise,然后返回一个新的(不同的) promise:

$scope.refresh: $ scope.refresh:

Same as you wrote. 和你写的一样。

$scope.getUserRecent: $ scope.getUserRecent:

function () {
  var id = window.localStorage.getItem('id');
  return new Promise(function(resolve, reject) {

    UserService
      .getTracks(id)
      .then(function (data) {
        var tracks = {}, i;

        console.log(data);

        for (i = 0; i < data.length; i++) {
          tracks[i] = SpotifyService.getTrack(i, data[i].uri, data[i].loved, data[i].from);
          console.log(i + ": " + tracks[i]);
        }

        console.log(tracks);

        resolve(tracks);

        })
        .catch(function (error){
          console.error(error);
          reject(error);
        });
    };

Some observation in the original OP's $scope.getUserRecent : 原始OP的$scope.getUserRecent中的一些观察结果:

  • Please note that then function has no return value, this way you output only undefined ; 请注意, then函数没有返回值,这样您只输出undefined
  • return tracks; written inside the then statement returns data only to the calling environment, ie $scope.getUserRecent and not the outer runtime environment that is the real goal. 写在then语句中的数据仅将数据返回到调用环境,即$scope.getUserRecent而不是真正的目标外部运行时环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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