对于循环不等待winjs承诺完成(For loop not waiting for winjs pr

2019-10-17 21:00发布

对于循环不等待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);
        });


    }

Answer 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
        });
    });
}


Answer 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);


文章来源: For loop not waiting for winjs promise completion