$ q.all是在返回前解决 [英] $q.all is resolving before the returns
问题描述
的 $ q.all
无论是其功能之前解决已经解决了。
The $q.all
is resolving before either of its functions has resolved.
我上传两个文件使用$阿贾克斯蔚蓝Blob存储(我不能得到$ HTTP工作):
I'm uploading two files to azure blob storage using $.ajax (I couldn't get $http to work):
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-点击
This is the function that sets up the $q.all
and is being called on a 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
<button ng-click='createVotation()'>Create</button>
的 q.all-&GT;然后
正在即使第一个 doPhoto
决心调用之前调用。我不知道是否有某些问题使用jQuery的Ajax,但我的理解是 $ q.all
应该等到双方的承诺(在我的案件有2个)进入它的然后之前完成
。
The q.all->then
is being called before even the first doPhoto
resolve is called. I'm not sure if there is some issue with using jQuery's ajax but it is my understanding that $q.all
should wait until both promises (in my case there are 2) are fulfilled before entering its then
.
作为一个附加的扭曲,这些照片的是正在上传,这样在工作,这是 $ q.all
未等待。
As an added twist, the photos are being uploaded so that is working, it is the $q.all
that is not waiting.
推荐答案
做一些研究和思考,从@charlietfl的各种意见后,只有这样我能得到$ q.all正常工作是做分配的功能打电话给一个变量,然后将它推入被传递到q.all数组。
After doing some research and thinking about the various comments from @charlietfl, the only way I could get the $q.all to work properly is do assign the function call to a variable and then push it into the array that is being passed to q.all.
for (var i in result.photos)
{
var output = doPhotos(result, i);
doPhotosArray.push(output);
}
这是我用来参考的各种例子似乎表明,在我原来的问题了code应该工作,但没有。
The various examples that I used for reference seem to indicate that the code in my original question should work but doesn't.
这篇关于$ q.all是在返回前解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!