Hello I'm new to NodeJs and am trying to work out the best way to get this chain of events working. I have to do two API calls get all the information I need. The first API call is just a list of IDs, then the second API call I pass the ID to get the rest of the information for each object. 

However using the method below, I have no idea when everything is finished. Please can someone help me out.

```
   function getData() { 
            var options = {
            method: 'GET',
            uri: 'https://api.call1.com',
                qs: {
                    access_token: _accessToken,
                }
            };
            request(options).then(function(apires){
               console.log("complete 1");
                var obj = JSON.parse(apires);
                obj.data.forEach(function(entry) {        
                    findMore(entry.id)
                });
            })
   }

   function findMore(id) {
            var options = {
            method: 'GET',
            uri: 'https://api.call2.com',
                qs: {
                    access_token: _accessToken,
                }
            };
            request(options).then(function(apires){
               console.log("complete 2");
                var obj = JSON.parse(apires);
            })
   }

```
['https://Stackoverflow.com/questions/45825412', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4071548/']
You can make your findMore method return a promise, so you can pass an array of those to Promise.all and handle the .then when all promises have finished.

```
function getData() { 
        var options = {
        method: 'GET',
        uri: 'https://api.call1.com',
            qs: {
                access_token: _accessToken,
            }
        };

        request(options).then(function(apires){
           console.log("complete 1");
            var obj = JSON.parse(apires);
            var promises = [];

            obj.data.forEach(function(entry) {        
                promises.push(findMore(entry.id));
            });

            return Promise.all(promises);
        })
        .then(function (response) {
             // Here response is an array with all the responses
             // from your calls to findMore
         })
 }

 function findMore(id) {
        var options = {
        method: 'GET',
        uri: 'https://api.call2.com',
            qs: {
                access_token: _accessToken,
            }
        };
        return request(options);
 }

```
EDIT: As others have mentioned, using a Promise.all() is likely a better solution in this case.

If you are open to using jQuery (a JavaScript library), then you can use the .ajaxStop() event handler and specify your own function. Sample code:

```
$(document).ajaxStop(function(){
    alert("All AJAX requests are completed.");
});

```

You will need to include the jQuery module. The instructions for Node.js are:

1. Install module through npm:

```
npm install jquery

```
2. Then use a "require" to use jQuery in your JavaScript code (a window with a document is required but there is no such "window" in Node so you can mock one with jsdom), see [npm - jQuery](https://www.npmjs.com/package/jquery) for details:

```
require("jsdom").env("", function(err, window) {
    if (err) {
        console.error(err);
        return;
    }

    var $ = require("jquery")(window);
});

```

If you want to stick to a pure JavaScript approach, you will need to create your own "module" to keep track of AJAX requests. In this module you can keep track of how many pending requests there are and remove them once they are terminated. Please see: [Check when all Ajax Requests are complete - Pure JavaScript](https://stackoverflow.com/questions/25971224/check-when-all-ajax-requests-are-complete-pure-javascript) for more details.