NodeJS Request return JSON from function

I've read a couple of posts about this here (callbacks) but I still don't really fully understand how to solve my problem. So I was hoping that somebody here could help me with mine and I would get it better.

Simple put I want the ID I get from the first request to be used for the second request.

I'm new to JavaScript and NodeJS in general.

function idRequest(name) {
    var options = {
        ...
    };
function callback(error, response, body) {
    if (response.statusCode == 200 && !error) {
       const info = JSON.parse(body);
       //console.log(info.accountId);
       return info.accountId;   
   }    
}
request(options, callback);

}

function requestById(accountId) {

var options = {
    ...
};

function callback(error, response, body) {
    if (response.statusCode == 200 && !error) {
        const info = JSON.parse(body);
        console.log(info);
    }   
  }

request(options, callback);

}

var id = idRequest(‘…’);
requestById(id);


#javascript #node-js

4 Likes2.60 GEEK