$ .each函数在完成处理所有内部函数之前返回值

时间:2017-11-07 08:55:39

标签: javascript knockout.js

我有一个uploadList对象填充了第2块中的代码,但是当我单步执行此代码时,我会间歇地看到return uploadList函数之前到达的issueRepository.GetIssuesOffline行。这意味着当我稍后循环遍历uploadList时(根据块1),r.amountr.returnsr.barcode为NULL,因为它们尚未设置(u.issue未定义)。

如果我在对Block 1的调用上放置了setTimeout,那么如果我给它足够长的时间进行处理,代码似乎会一直执行,但没有SetTimeout uploadList看起来几乎就像在等待所有以前的函数完成之前异步返回。

任何人都可以看到我在这里缺少的内容,并理解为什么在第2块完成并返回之前processIssues函数中不会定义问题对象?

第1区:

    var returns = Enumerable
        .From(uploadList)
        .Select(function (u) {
            var i = u.issue();
            var r = i.Returns();

            return {
                amount: r.amount,
                Totes: r.totes,
                barcode: r.barcode,
                Depo: i.supplier == null ? 'Unknown supplier' : i.supplier.name,
                StatusID: i.Status.Code,
                SupplierID: i.supplier.id,
                DepoFullName: i.depot
            }
        })
        .ToArray();

第2栏:

me.getIssuesFromReturnsList = function () {
    var uploadList = [];
    $.each(me.returnsList(), function (index, i) {
        var issue = issueRepository.GetDefaultIssue();
        issue.productname = "Loading from server...";
        issue.barcode = i.barcode;
        issue.ReturnsAmount(i.amount);

        var uploadItem = {
            barcode: i.barcode,
            amount: i.amount,
            issue: ko.observable(issue)
        };

        uploadList.push(uploadItem);

        issueRepository.GetIssuesOffline(i.barcode, function (issues) {
            if (issues.length > 0) {
                uploadItem.issue(issues[0]);
            }
        });
    });

    return uploadList;
}

其中一些功能的代码供参考,如下所示:

GetIssuesOffline:

    GetIssuesOffline: function (barcode, callback) {
        var me = this;

        issueDatabase.GetIssues(barcode, function (issues) {
            me._processIssues(issues);

            return callback(issues);
        });
    }

issueDatabase.getIssues:

    GetIssues: function (barcode, callback) {
        var me = this;
        db.transaction(
            function (context) {
                var query = "SELECT * FROM issues WHERE barcode LIKE '" + barcode + "%' ORDER BY offSaleDate DESC LIMIT 25";
                context.executeSql(query, [], function (context, result) {
                    var issues = [];

                    for (var i = 0; i < result.rows.length; i++) {
                        var issue = result.rows.item(i);
                        issue.Source = dataSources.Local;
                        issue.isRanged = issue.isRanged == 1 ? true : false;
                        issues.push(issue);
                    }

                    callback(issues);
                }, me.ErrorHandler);
            }
            , me.ErrorHandler);
    }

me.processIssues:

    _processIssues: function (issues) {
        var me = this;
        $.each(issues, function (index, i) {
            if (i.issueNumber == null) {
                i.issueNumber = '';
            }

            i.issueNumber = i.issueNumber + '';
            i.productNumber = i.productNumber + '';

            if (i.issueNumber.length == 1) {
                i.issueNumber = '0' + i.issueNumber;
            }

            i.barcode = parseInt(i.productNumber + '' + i.issueNumber);
            i.Status = me.GetIssueStatus(i);
            i.supplier = me.GetissueSupplierDetails(i);
            i.ReturnsAmount = ko.observable(0);
            i.Returns = ko.observable({ totes: [] });

                returnsRepository.GetReturn(i.barcode, function (r) {
                    i.ReturnsAmount(r.amount);

                    if (r.amount > 0) {
                        i.Returns(r);
                    } else {
                        i.Returns({ totes: [] });
                    }
                });
            };
            i.RefreshReturnsAmount();

            me.IssueDatabase.UpdateIssue(i, function (issue) {
                me.IssueDatabase.UpdateIssueLastUpdated(issue);
            });
        });
    }

0 个答案:

没有答案