I have as main function as follows:
function main(body) {
    var deferred = Q.defer();
    // Make an API request
    console.log("body :"+ body);
    var jsonbody = JSON.parse(body);
    partialSequence = jsonbody['partialSequence'];
    uniqID =  jsonbody['uniq'];
    resultLength = jsonbody['resultLength'];
    console.log("resultLength :"+ resultLength);
    if (partialSequence.indexOf("G") > -1) ns.push("G");
    if (partialSequence.indexOf("A") > -1) ns.push("A");
    if (partialSequence.indexOf("C") > -1) ns.push("C");
    if (partialSequence.indexOf("T") > -1) ns.push("T");
    uniq = uniqID;
    var sequence = Promise.resolve();
    for (var i = 0; i < resultLength; i++) {
      location = i;
      for (var j = 0; j < nuclides.length; j++) {
        n = nuclides[j]
        var promise = getLocation(n, location, uniq);
        promise.then(function(values) {
          console.log("location :"+values[0] + " "+ values[1]);
          if (expressed) {
            isExpressed = true;
            if(route > 0) {
              for (var key in resultSeq) {
                if (resultSeq.hasOwnProperty(key)) {
                  var temp = resultSeq[key]
                  delete resultSeq[key];
                  temp = temp.concat(n);
                  resultSeq[temp] = temp;
                }
              }
            } else {
              resultSeq[n] = n;
            }
          }
        });
      }
      if (isExpressed) route++; //used to check if we append to existing sequences.
    }
    deferred.resolve();
    return deferred.promise
}
function getLocation(n, location, uniq) {
  var expressed
  var deferred = Q.defer();
  Q.ninvoke(request, 'get', {
    url:         "https://myapi.com/location?"+"location="+location+"&"+"nucleotide="+n+"&"+"uniq=    "+uniq
  }).spread(function(response, body) {
    expressed=1;
    var jsonbody = JSON.parse(body);
    return [jsonbody["expressed"], location];
  });
  return deferred.promise
}
When I console.log the location values[0] is out of order when it should be 0,1,2.......n. How can I achieve this? Thanks a lot!
 
     
    