-2

how call a callback function, after all the functions are finished?

All functions must start at the same time, and when all functions finish running, run the callback

function step_one(callback){
    parse1site();
    parse2site();
    parse3site();
    parse4site(); 
    parse5site();
    parse6site();
    parse7site();
    parse8site();
    parse9site();
    parse10site(); 
    parse11site();
    parse12site();
    parse13site();
    parse14site(); 
    parse15site();
    parse16site();
    parse17site();
    parse18site();
    parse19site();
    parse20site();
}

Example function

function parse1site(){
    var URL = "https://site1.com";
    needle.get(URL, function(error, response){
        if (!error && response.statusCode == 200){
            data["site1"] = response.body;
            console.log("OK");
        } else{
            console.log("error");
        }
    });
}
neieugene
  • 3
  • 2
  • See [ask] and [mcve] on tips to how to ask a good question. What have you tried already? Also, what do the `parseXsite` functions do? Do they return a promise or are they synchronous code? There's a lot of unknowns for people to help you – Horia Coman Nov 21 '17 at 16:41
  • 2
    Possible duplicate of [How can I wait for set of asynchronous callback functions?](https://stackoverflow.com/questions/10004112/how-can-i-wait-for-set-of-asynchronous-callback-functions) – JJJ Nov 21 '17 at 16:41
  • You haven’t shown us what those functions look like. – Philip Brack Nov 21 '17 at 16:52
  • @PhilipBrack Added an example function – neieugene Nov 21 '17 at 17:11

1 Answers1

1

I would change your usage of needle to the promise API and then use Promise.all

var p1 = needle('get', 'https://server.com/posts/12');
var p2 = needle('get', 'https://server.com/posts/13');
//...
Promise.all([p1,p2]).then((data)=>{
  // here you will get the response of all of your requests in array data
});
Philip Brack
  • 1,340
  • 14
  • 26