robe007
robe007

Reputation: 3927

Return an object 'ordered' with bluebird using Promise.map

I have an array of numbers that I pass to an api url. It returns the answer on an object, but the problem is that I want this object ordered according to the original array.

This is my code:

var Promise = require("bluebird");
var cheerio = require('cheerio');
var request = Promise.promisifyAll(require("request"));

var numbers = ['22222', '33333', '44444'];
var obj = {};

Promise.map(numbers, function(number) {
    var options = {
        url: 'http://example.com/api',
        method: 'POST',
        form: {'number': number}
    };
    return request.postAsync(options).spread(function(response, body){
        if (response.statusCode == 200){
            var $ = cheerio.load(body);
            var link = $('script').get()[5].children[0].data;
            var regexNumber = new RegExp('<strong><h1>(.*?)</h1></strong>');
            var result = (regexNumber.exec(link)[1]);
            obj[number] = result;
        }
        else {
            console.log('Error');
        }
    });
}, {concurrency: 1}).then(function() {
    console.dir(obj);
});

This is what throws:

{ '33333': 'a text',
  '44444': 'a text',
  '22222': 'a text' }

I want it on this way: (according to the order of the original array)

{ '22222': 'a text',
  '33333': 'a text',
  '44444': 'a text' }

I have tried also with this other code, and the same happens:

var numbers = ['22222', '33333', '44444'];
var obj = {
    number: [],
    text:
};

var links = numbers.each(function(number) {
    var options = {
        url: 'http://example.com/api',
        method: 'POST',
        form: {'number': number}
    };
    return request.postAsync(options).spread(function(response, body){
        if (response.statusCode == 200){
            var $ = cheerio.load(body);
            var link = $('script').get()[5].children[0].data;
            var regexNumber = new RegExp('<strong><h1>(.*?)</h1></strong>');
            var result = (regexNumber.exec(link)[1]);
            obj.number.push(number);
            obj.text.push(result);
            return obj;
        }
        else {
            console.log('Error');
        }
    });
});

Promise.all(links).then(function(results) {
    results.forEach(function (data, i){
        console.log(i + ' => ' + data.number[i] + ' => ' + data.text[i]);
    });
});

Some ideas?

Upvotes: 0

Views: 429

Answers (1)

Bergi
Bergi

Reputation: 664297

You should use mapSeries, which iterates your collection in order (which map doesn't guarantee, being parallel after all).

var numbers = ['22222', '33333', '44444'];

Promise.mapSeries(numbers, function(number) {
    return request.postAsync({
        url: 'http://example.com/api',
        method: 'POST',
        form: {'number': number}
    }).spread(function(response, body){
        if (response.statusCode == 200){
            var $ = cheerio.load(body);
            var link = $('script').get()[5].children[0].data;
            var regexNumber = new RegExp('<strong><h1>(.*?)</h1></strong>');
            return regexNumber.exec(link)[1];
        } else {
            console.log('Error'); // throw it?
        }
    });
}).then(function(results) {
    for (var i=0; i<results.length; i++)
       console.log(i + ' => ' + numbers[i] + ' => ' + results[i]);
});

Upvotes: 3

Related Questions