<commit_before>var _ = require('lodash');
var util = require('util');
var request = require('request');
var q = require('q');
var async = require('async-q');

var getMessage = function (msgId) {
  var defer = q.defer();
  request({url: util.format('http://localhost:3000/messages/%s/', msgId),
           method: 'GET',
           headers: {'Content-Type': 'application/json'} },
    function (err, response, body) {
      if (err) {
        console.log("Failed to get message: ", err);
        defer.reject(err);
      }
      else {
        //console.log("Got message: ", body);
        defer.resolve(body);
      }
    }
  );
  return defer.promise;
};

var msgIds = ['0','1','2','3'];

async.map(msgIds, function (msgId) {
  return getMessage(msgId);
}).then(
  function (messageList) {
    var parsedList = _.map(messageList, function (msgString) { return JSON.parse(msgString); });
    console.log(parsedList);
  },
  function (err) {
    console.log("ERROR: ", err);
  }
).done();
<commit_msg>Refactored.<commit_after>var _ = require('lodash');
var util = require('util');
var request = require('request');
var q = require('q');
var async = require('async-q');

var getMessage = function (msgId) {
  var defer = q.defer();
  request({url: util.format('http://localhost:3000/messages/%s/', msgId),
           method: 'GET',
           headers: {'Content-Type': 'application/json'} },
    function (err, response, body) {
      if (err) {
        console.log("Failed to get message: ", err);
        defer.reject(err);
      }
      else {
        defer.resolve(body);
      }
    }
  );
  return defer.promise;
};

var msgIds = ['0','1','2','3'];

async.map(msgIds, function (msgId) {
  return getMessage(msgId).then(
    function (msg) {
      return JSON.parse(msg);
    });
}).then(
  function (messageList) {
    console.log(messageList);
  },
  function (err) {
    console.log("ERROR: ", err);
  }
).done();

{'fraction_non_alphanumeric': 0.190625, 'fraction_numerical': 0.008333333333333333, 'mean_word_length': 2.977225672877847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6224142', 'n_tokens_mistral': 641, 'n_tokens_neox': 624, 'n_words': 138}