I am new to promises and stuck in the next exercise.
I have an array of values, and I want to make an asynchronous call for each of them. In the callback, I want to make another call based on the result of the first call.
Basically, my disappointment is this: The execution order should be "1x2x3x", but the order is "123xxx"
In other words, the loop is already going to the next iteration when the sub / nested promise of the first promise is not yet filled.
var values = ["1", "2", "3"]; function do(val) { var deferred = Q.defer(); asyncCall(val) .then( function( response ) { console.log(val); asyncCall(response) .then( function ( response ) { console.log('x'); deferred.resolve(true) }); }); return deferred.promise; } var result = do(values[0]); values.forEach( function(f) { result = result.then(do(f)); }
This may be a simple solution, but I am stuck on it.
javascript promise loops nested order
strai
source share