$ q.all在退货前解决

$q.all在其任一函数解决之前解析。

我使用$ .ajax将两个文件上传到azure blob存储(我无法获得$ http工作):

  function doPhotos (result, i) { var d = $q.defer(); var requestData = new Uint8Array($scope.files[i].postArray); $.ajax({ url: result.photos[i].imageUri, type: "PUT", data: requestData, processData: false, beforeSend: function (xhr) { xhr.setRequestHeader('x-ms-blob-type', 'BlockBlob'); xhr.setRequestHeader('x-ms-blob-content-type', $scope.files[i].type); xhr.setRequestHeader('x-ms-meta-uploadvia', 'CORS Demo'); xhr.setRequestHeader('Content-Length', requestData.length); }, success: function (data, status) { d.resolve(data); }, error: function (xhr, desc, err) { console.log('error uploading photo ' + desc); d.resolve(err); } }); return d.promise; } 

这是设置$q.all并在ng-click上调用的函数:

 $scope.createVotation = function () { services.photoset.create($scope.model).then(function (result) { $scope.model.id = result.id; var doPhotosArray= []; for (var i in result.photos) { doPhotosArray[i] = doPhotos(result, i); } $q.all(doPhotosArray).then(function (data) { // this is being called almost immediately before the photos upload $scope.safeApply(function () { $scope.submitting = false; $location.path('/vote/update/' + $scope.model.id); }); }); }); } }; 

HTML:

  

q.all->then在调用第一个doPhoto resolve之前调用q.all->then 。 我不确定使用jQuery的ajax是否存在一些问题但是我的理解是$q.all应该等到两个promises(在我的情况下有2个)在进入它之前完成。

作为一个额外的扭曲,照片正在上传,所以这是工作,它是$q.all ,而不是等待。

在做了一些研究并考虑@charlietfl的各种注释后,我能让$ q.all正常工作的唯一方法就是将函数调用赋给变量然后将其推送到传递给q的数组中。 。所有。

 for (var i in result.photos) { var output = doPhotos(result, i); doPhotosArray.push(output); } 

我用于参考的各种示例似乎表明我原始问题中的代码应该可以工作但不能。