【问题标题】:For loop not waiting for winjs promise completionFor循环不等待winjs承诺完成
【发布时间】:2013-03-23 06:14:23
【问题描述】:

for 循环不等待 winjs 承诺完成

for (var j = 0; j < magazineResult[0].data.length; j++) {
        downRequest[0].data[j].COVER_PAGE_THUMB = parentUrl + eval(JSON.stringify(downRequest[0].data[j].COVER_PAGE_THUMB));

        // Create a new download operation.
        downloadFile(eval(magazineResult[0].data[j].COVER_PAGE_THUMB),eval(JSON.stringify(magazineResult[0].data[j].COVER_PAGE_THUMB)));
        var url = downRequest[0].data[j].COVER_PAGE_THUMB;
        var imgPath = downRequest[0].data[j].ISSUE_ID;
        var imgExtension = url.substring(url.lastIndexOf('.') + 1);
        var fileName = imgPath + "." + imgExtension;
        var promise = Windows.Storage.ApplicationData.current.localFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting);
        // Assign the completion handler function.
        promise.done(function (newFile) {
            MagazineDownLoad.downloadFile(url, fileName, j, newFile);
        });


    }

【问题讨论】:

  • WinJS 承诺在哪里?
  • 我需要为 MagazineDownLoad.downloadFile 操作实现承诺。只有在此操作完成后for循环才会继续。如何在这里实现承诺..?

标签: microsoft-metro winjs


【解决方案1】:

如果你想让MagazineDownLoad.downloadFile 异步操作,那么你必须修改它的定义:

// in MagazineDownload
function downloadFile(url, filename, j, newfile){
    return new WinJS.Promise(function (complete, error, progress) {
        var returnValue;
        //do the stuff that you do and assign something to returnValue
        complete(returnValue);
    });
}

然后就可以异步使用了:

for (var j = 0; j < magazineResult[0].data.length; j++) {
    downRequest[0].data[j].COVER_PAGE_THUMB = parentUrl + eval(JSON.stringify(downRequest[0].data[j].COVER_PAGE_THUMB));

    // Create a new download operation.
    downloadFile(eval(magazineResult[0].data[j].COVER_PAGE_THUMB),eval(JSON.stringify(magazineResult[0].data[j].COVER_PAGE_THUMB)));
    var url = downRequest[0].data[j].COVER_PAGE_THUMB;
    var imgPath = downRequest[0].data[j].ISSUE_ID;
    var imgExtension = url.substring(url.lastIndexOf('.') + 1);
    var fileName = imgPath + "." + imgExtension;
    var promise = Windows.Storage.ApplicationData.current.localFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting);
    // Assign the completion handler function.
    promise.done(function (newFile) {
        MagazineDownLoad.downloadFile(url, fileName, j, newFile).done(function(result){
            //do some more stuff with the result
        });
    });
}

【讨论】:

    【解决方案2】:

    WinJS.Promise() 异步运行,您的 for 循环同步运行。您正在经历的事情是意料之中的。如果您想将您的操作排队,您不应该执行循环,而是在您的done() 被调用时排队一个新操作。像这样的:

    var index = 0, data = magazineResult[0].data;
    function queueDownload() {
        // Duplicate all needed logic here from your question
        var promise = Windows.Storage.ApplicationData.current.localFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting);
        // Assign the completion handler function.
        promise.done(function (newFile) {
            MagazineDownLoad.downloadFile(url, fileName, j, newFile);
            if (index < data.length) {
                queueDownload(++index);
            }
        });
    }
    queueDownload(index);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 2018-04-25
      • 2018-03-05
      • 2017-12-30
      • 2016-09-09
      • 1970-01-01
      相关资源
      最近更新 更多