-1

How can I return a object of data returned by asynchronous function called multiple times from within a asynchronous function.

I'm trying to implement like this :

var figlet = require('figlet');

function art(dataToArt, callback)
{ 

var arry[];

    figlet(dataToArt, function(err, data) { 
        if (err) { 
            console.log('Something went wrong...'); 
            console.dir(err); 
            return callback(''); 
        } 
    arry[0] = data;
        callback(arry);
    });


figlet(dataToArt, function(err, data) { 
        if (err) { 
            console.log('Something went wrong...'); 
            console.dir(err); 
            return callback(''); 
        } 
    arry[1] = data;
        callback(arry);
    });

} 


art('Hello World', function (data){
    console.log(data);
});

How can I do it correctly, I searched and searched but couldn't find a solution.

Ps. I'm using Figlet.js

Garvit Jain
  • 1,862
  • 2
  • 19
  • 27
  • 1
    To clarify what you want: You're calling something asynchronously more than once, and want to get both/all responses into an array and only call the callback once with the array with both/all items populated? – James Thorpe Mar 09 '16 at 16:36
  • Yes, thats exactly what I want – Garvit Jain Mar 09 '16 at 16:37
  • Try to look into promises and this: http://stackoverflow.com/questions/10004112/how-can-i-wait-for-set-of-asynchronous-callback-functions question. – PNS Mar 09 '16 at 16:45

2 Answers2

0

I don't know if you're ok using an external module, but you can use tiptoe.

Install it using npm install tiptoe like any regular module and it basically goes like this:

var tiptoe = require('tiptoe')

function someAsyncFunction(obj, callback) {
  // something something
  callback(null, processedData);
}

tiptoe(
  function() {
    var self = this;
    var arr = ['there', 'are', 'some', 'items', 'here'];
    arr.forEach(function(item) {
      someAsyncFunction(item, self.parallel());
    });
  },
  function() {
    var data = Array.prototype.slice.call(arguments);
    doSomethingWithData(data, this);
  },
  function(err) {
    if (err) throw (err);
    console.log('all done.');
  }
);

the someAsyncFunction() is the async function you want to call does something and calls the callback parameter as a function with the parameters error and data. The data parameter will get passed as an array item to the following function on the tiptoe flow.

Sergio Moura
  • 4,888
  • 1
  • 21
  • 38
  • I wasn't able to implement it.. I did it using [this](http://mostafa-samir.github.io/async-iterative-patterns-pt1/) – Garvit Jain Mar 10 '16 at 07:41
0

Did it Myself :) Thanks to mostafa-samir's post

var figlet = require('figlet');

function WaterfallOver(list, iterator, callback) {

    var nextItemIndex = 1;
    function report() {
        nextItemIndex++;
        if(nextItemIndex === list.length)
            callback();
        else
            iterator([list[0],list[nextItemIndex]], report);
    }
    iterator([list[0],list[1]], report);
}

var FinalResult = [];

WaterfallOver(["hello","Standard","Ghost"], function(path, report) {
    figlet.text(path[0], { font: path[1]  }, function(err, data) {
      if (err) {
          FinalResult.push("Font name error try help");
          report();
          return;
      }
      data = '<pre>.\n' + data + '</pre>';
      FinalResult.push(data);
      report();
  });
}, function() {
    console.log(FinalResult[0]);
    console.log(FinalResult[1]);
});
Garvit Jain
  • 1,862
  • 2
  • 19
  • 27