0

I need pass the reference of i to .then callback

var promises = loadScript(assets["js"][0]);
  for (var i = 1; i < assets["js"].length; i++){
    promises = promises.then(function() { loadScript(assets["js"][i]) });
  }

I tried this:

var promises = loadScript(assets["js"][0]);
  for (var i = 1; i < assets["js"].length; i++){
    promises = promises.then((function(i){loadScript(assets["js"][i])})(i));
  }

Here the function is executed before the .then method call

Return from the then

var promises = loadScript(assets["js"][0]);
  for (var i = 1; i < assets["js"].length; i++){
    promises = promises.then( function() { return loadScript(assets["js"][i]) });
  }

I get the error:

i = undefined
  • You're not `return`ing from the `then`. – Benjamin Gruenbaum Jan 15 '17 at 16:40
  • @BenjaminGruenbaum — They're not trying to do anything with the return value … so what does that matter? – Quentin Jan 15 '17 at 16:46
  • I dont understand.. This is a promise, why a i need do something with the return? I just use .then and finally i can use $q.all(), not? I need the specific idx of loop to start a promise based on array value – user3251174 Jan 15 '17 at 18:02

0 Answers0