1

I'm trying to implement a function, which wait for all my fileUpload() works complete. Each work is monitored through the jQuery's Promise mechanism. Below is my first attempt to monitor all of the upload work. (fileUpload() function is emulated with setTimeout() for testing)

var numOfFiles = 4;

function updateFile(filename, num)
{
  console.log('++updateFile ' + filename);
  var d = new $.Deferred;
  setTimeout(function(){
    console.log('--updateFile ' + filename);
    if(num == 100) {
        d.reject("Error");
    } else {
        d.resolve();
    }
  }, 1000);
  return d.promise();
}

var promiseList = [];
for(var i=0; i < numOfFiles; i++) {
  promiseList[i] = updateFile("foo_" + i + ".txt", i);
}
// $.when( promiseList[0], promiseList[1], promiseList[2], promiseList[3] ) // This works, but not scalable.
$.when( ...promiseList )        // This works with Chrome, but not IE
.done(function(){ console.log("all pass"); })
.fail(function(e){ console.log("fail:", e); });

console.log("end");

This works fine with Chrome browser with the following output.

++updateFile foo_0.txt
++updateFile foo_1.txt
++updateFile foo_2.txt
++updateFile foo_3.txt
end
--updateFile foo_0.txt
--updateFile foo_1.txt
--updateFile foo_2.txt
--updateFile foo_3.txt
all pass

However, it fails as syntax error with the IE11. Over several internet search, I learned the IE11 doesn't support the array spread operation. Can anyone please help me how how this can be workarounded.

Based on this article, Alternatives of spread syntax

I tried this

spreadList = [];
Array.prototype.push.apply(spreadList, promiseList);
$.when(spreadList)

But the "all pass" appeared earlier than completion of each updateFile() functions.

++updateFile foo_0.txt
++updateFile foo_1.txt
++updateFile foo_2.txt
++updateFile foo_3.txt
all pass
end
--updateFile foo_0.txt
--updateFile foo_1.txt
--updateFile foo_2.txt
--updateFile foo_3.txt

Thanks for your help !

Tony
  • 363
  • 3
  • 13

0 Answers0