Thursday, 25 May 2017

Loop with promises

Given the below code:

var arr = ['one', 'two']

for (index in arr) {
  console.log('outside promise:', arr[index])

  myPromise().then(function(response) {
    console.log('inside promise:', arr[index])
  })
}

My output:

// outside promise: one
// outside promise: two
// inside promise: one
// inside promise: one

Why the hack the console output inside the promise doesn't loop trough values?



via Thiago Régis

No comments:

Post a Comment