angularJS $q -- 停止执行以等待所有承诺
angularJS $q -- stopping execution to wait for all promises
我遇到了这个问题,我无法通过谷歌搜索找到解决方案。
我有一个库,我正在使用(并且不想编辑,除非真的有必要)它允许用户 select 一个项目,然后调用我的自定义回调函数来修改项目,然后继续使用它。
我需要对其执行一些异步任务,这可能需要一些时间。这会造成竞争条件,因为当回调函数完成时我的异步任务尚未完成并且库继续处理该项目。
library.onItemSelectionCallback = function (item) {
myService.modifyItem(item).then(
function (modifiedItemProperty) {
item.newProperty = modifiedItemProperty;
});
myService.anotherModifyItem(item).then(
function (modifiedItemProperty) {
item.existingProperty = modifiedItemProperty;
});
}
如何在允许此回调完成之前等待我的两个异步任务完成?
我唯一能想到的就是循环 while 并每隔一百毫秒左右休眠一次,直到两个 promise 都已解决,但这似乎不是一个很好的解决方案。
我知道这会使异步请求非常同步,并且可能对用户体验有害,但我真的看不到其他出路。
编辑: 我知道我冒着删除问题的一般性质并因此使其过于本地化的风险,我会说我正在尝试使用 angular-file-upload module, specifically, trying to mount a custom imageService
, that would resize the picture before it's upload. I'm mounting it on the onBeforeUploadItem回调。这个想法是创建调整大小的图像可能需要一段时间,这就是为什么我需要 return 我的 imageService
的承诺,需要在上传之前解决。
如果 modifyItem
和 anotherModifyItem
独立工作(即一个不依赖另一个),您可以将它们都通过管道传输到 $q.all
,例如
library.onItemSelectionCallback = function(item) {
var promises = {
newProperty: myService.modifyItem(item),
existingProperty: myService.anotherModifyItem(item)
};
return $q.all(promises).then(function(values) {
return angular.extend(item, values);
});
}
这将 return 以 item
解决的承诺。
对于我问题的第一部分 -- 是的,我想真正等待这两个承诺得到解决的唯一方法是使用 while
和 sleep
,使它们成为现实同步的,这可能会起作用,甚至不会那么糟糕(除了网站暂停直到请求得到满足),但会让我对自己作为一个人以及我的行为如何影响这个世界感到非常非常糟糕。
如果没有 hack afaik,就不可能正确地混合回调和承诺。
对于我的问题的第二部分——根据@georgeawg 的评论,认为 AngularJS 模块实现了 HTML5 API
和回调而不是 $http
服务和承诺这不是一个好的 AngularJS 模块应该如何实现,所以我转向了一个不同的模块 ng-file-upload,尽管有人可能会说它不太时尚,但它做得很好并且在 ng-file-upload =24=] 方式(ng-file-upload 提供了一个简单的 $upload
服务,即 returns 一个承诺。如果你想在上传前修改文件,建议的方式是简单地 $watch 并抓紧时间用户拖放或选择文件。)。
我遇到了这个问题,我无法通过谷歌搜索找到解决方案。
我有一个库,我正在使用(并且不想编辑,除非真的有必要)它允许用户 select 一个项目,然后调用我的自定义回调函数来修改项目,然后继续使用它。
我需要对其执行一些异步任务,这可能需要一些时间。这会造成竞争条件,因为当回调函数完成时我的异步任务尚未完成并且库继续处理该项目。
library.onItemSelectionCallback = function (item) {
myService.modifyItem(item).then(
function (modifiedItemProperty) {
item.newProperty = modifiedItemProperty;
});
myService.anotherModifyItem(item).then(
function (modifiedItemProperty) {
item.existingProperty = modifiedItemProperty;
});
}
如何在允许此回调完成之前等待我的两个异步任务完成?
我唯一能想到的就是循环 while 并每隔一百毫秒左右休眠一次,直到两个 promise 都已解决,但这似乎不是一个很好的解决方案。
我知道这会使异步请求非常同步,并且可能对用户体验有害,但我真的看不到其他出路。
编辑: 我知道我冒着删除问题的一般性质并因此使其过于本地化的风险,我会说我正在尝试使用 angular-file-upload module, specifically, trying to mount a custom imageService
, that would resize the picture before it's upload. I'm mounting it on the onBeforeUploadItem回调。这个想法是创建调整大小的图像可能需要一段时间,这就是为什么我需要 return 我的 imageService
的承诺,需要在上传之前解决。
如果 modifyItem
和 anotherModifyItem
独立工作(即一个不依赖另一个),您可以将它们都通过管道传输到 $q.all
,例如
library.onItemSelectionCallback = function(item) {
var promises = {
newProperty: myService.modifyItem(item),
existingProperty: myService.anotherModifyItem(item)
};
return $q.all(promises).then(function(values) {
return angular.extend(item, values);
});
}
这将 return 以 item
解决的承诺。
对于我问题的第一部分 -- 是的,我想真正等待这两个承诺得到解决的唯一方法是使用 while
和 sleep
,使它们成为现实同步的,这可能会起作用,甚至不会那么糟糕(除了网站暂停直到请求得到满足),但会让我对自己作为一个人以及我的行为如何影响这个世界感到非常非常糟糕。
如果没有 hack afaik,就不可能正确地混合回调和承诺。
对于我的问题的第二部分——根据@georgeawg 的评论,认为 AngularJS 模块实现了 HTML5 API
和回调而不是 $http
服务和承诺这不是一个好的 AngularJS 模块应该如何实现,所以我转向了一个不同的模块 ng-file-upload,尽管有人可能会说它不太时尚,但它做得很好并且在 ng-file-upload =24=] 方式(ng-file-upload 提供了一个简单的 $upload
服务,即 returns 一个承诺。如果你想在上传前修改文件,建议的方式是简单地 $watch 并抓紧时间用户拖放或选择文件。)。