Javascript 如何使用异步操作WinJS在for循环后执行操作

Javascript 如何使用异步操作WinJS在for循环后执行操作,javascript,asynchronous,winjs,Javascript,Asynchronous,Winjs,我在WinJS上有以下代码块: // Triggers SOAP requests depending of how many webServices are required for uploading all the pictures for (var i = 0; i < arrayCaptures.length; i++) { callWS(arrayTextFieldValues[i], UID_KEY[7], arrayCaptures[i].name).then(f

我在WinJS上有以下代码块:

// Triggers SOAP requests depending of how many webServices are required for uploading all the pictures
for (var i = 0; i < arrayCaptures.length; i++) 
{
    callWS(arrayTextFieldValues[i], UID_KEY[7], arrayCaptures[i].name).then(function (response) 
    {
        if (response == true) 
        {
            //if true, we have store the id of the picture to delete
            deletedCapturesIndexesArray.push(i);
        }
    },
    function (error) { }
    );
}

//my next action comes after this for loop
removeCapturesOfScreenWithIndexArray(deletedCapturesIndexesArray);
//根据上传所有图片需要多少Web服务触发SOAP请求
对于(var i=0;i
它的功能:它使用异步操作(SOAP WebService调用)执行一段代码,并在第二个线程中使用IndexArray执行removeCapturesOfScreenWithIndexArray


我需要的是这个程序只在for循环中的所有操作都完成后才执行我的下一个操作(removeCapturesOfScreenWithIndexArray),我认为这与承诺主题有关,但我不清楚,怎么做???

如果你想在承诺完成后发生什么事情,您需要附加到承诺的
,然后
。如果您希望在多个承诺全部完成后发生某些事情,您可以将这些承诺加入到单个组合承诺中,然后附加到组合承诺的
then

您的代码还有一个bug,它捕获了循环变量。这意味着
deleteCapturesIndexArray.push(i)
将始终推送
arrayCaptures.length

这两个问题都有解决办法

// Triggers SOAP requests depending of how many webServices are required for uploading all the pictures
var promiseArray = arrayCaptures.map(function(capture, i) {
    return callWS(arrayTextFieldValues[i], UID_KEY[7], capture.name).then(function (response) 
    {
        if (response == true) 
        {
            //if true, we have store the id of the picture to delete
            deletedCapturesIndexesArray.push(i);
        }
    },
    function (error) { }
    );
});

// Run some more code after all the promises complete.
WinJS.Promise.join(promiseArray).then(function() {
    removeCapturesOfScreenWithIndexArray(deletedCapturesIndexesArray);
});

我需要等待所有的承诺都被履行,以执行下一行代码removeCapturesOfScreenWithIndexArray(DeletedCapturesIndexArray);但是我当前的代码跳到下一步,如何强迫它等待所有的承诺???你能帮我举个例子吗?可能重复的(仅供参考,我很感激你是WinJS新手,并试图找出一些棘手的事情,但请在发布新问题之前在google、SO或MSDN上搜索一下。