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/']
A couple of things to think about:

**If you care about the fate of a promise, always return it**.

In your case, `findMore` does not return the promise from `request`, so `getData` has no handle to track the resolution (or rejection) of that promise.

**You can track the resolution of multiple promises with [Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all).**

> 
> The Promise.all() method returns a single Promise that resolves when all of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with the reason of the first promise that rejects.
> 
> 
> 

Lets put these to use on your example:

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

    return request(options)
        .then(function(apires){
            var obj = JSON.parse(apires);
            var findMorePromises = obj.data.map(function(entry) {        
                return findMore(entry.id)
            });
            return Promise.all(findMorePromises);
        })
}

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

    return request(options)
        .then(function(apires){
           return JSON.parse(apires);
        })
}

```

I've used [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) to construct the array of promises, but you could just as well use a `foreach` and push into an array similar to be more similar to your example code.

It's also good practice to make sure you are handling rejection of any promises (via `catch`), but I'll assume that is out of the scope of this question.
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.