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);
 }

```
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.