【问题标题】:async js callback for each upload每次上传的异步 js 回调
【发布时间】:2015-08-22 19:25:46
【问题描述】:

我是网络开发的新手。我遇到了以下问题:

我在 amazon s3 上传图片并收到包含此图片链接的响应的回调。然后我将它推入数组并将这个数组存储在数据库中。但是我不知道在所有图像上传后如何准确地转到下一个功能。

在这个块中,当计数数组中下载图像的长度等于响应中收到的链接数时,我将转到瀑布中的下一个函数。但由于某种原因,它只能下载奇数个图像。

你怎么做才能在我得到所有东西之后才能在瀑布中继续下一个功能

$scope.createPromo = function () {
    async.waterfall([
        function(callback) {
            var promImageLoc = [];
            var upload = $scope.uploaderPromo.queue;
            $scope.uploaderPromo.uploadAll();
            $scope.uploaderPromo.onSuccessItem = function(fileItem, response, status, headers) {
                console.log('success response', response);
                    promImageLoc.push(response.location);
                    if (upload.length === promImageLoc.length) {
                    callback(null, promImageLoc);
                }
            }
        },

【问题讨论】:

    标签: javascript asynchronous callback angular-file-upload


    【解决方案1】:

    我从未使用过 async-waterfall,但如果必须这样做,我会使用 $q 承诺,当收到 resolve 事件时,您可以调用所需的函数。

    开始here。

    另外,查找$q.all() - 这将对您有所帮助。
    这是一个带有成功事件的 $q.all() 示例(当所有 http 请求都完成时):Is there a second success in angular $q.all() as in jQuery $.get()

    【讨论】:

    • 是的,我刚才在考虑使用 promises 的可能性(比如 bluebird)。我会显示工作代码(如果将来有人突然需要)
    猜你喜欢
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 2015-12-31
    • 2013-04-27
    • 1970-01-01
    • 2019-02-12
    • 2021-12-03
    • 1970-01-01
    相关资源
    最近更新 更多