TeaSeaPea_IP
TeaSeaPea_IP

Reputation: 3

Node.js Access Values from Anonymous function

Good morning!

I've been struggling to get a specific value returned from my function:

const getFolders = function (PID){
var token = getStoredToken()
request.get({
    url: 'https://api.procore.com/vapid/folders',
    headers: {
        Authorization: "Bearer " + token.access_token
    }, 
    json: {
        company_id: '12594',
        project_id: PID
    }
}, function test(err, response, body){
    return body
})
// I NEED THE RETURN VALUE OF THE ABOVE FUNCTION HERE SO I CAN ACCESS WHEN CALLING getFolders()
}

Is this possible? If so, how?

Thanks!

Upvotes: 0

Views: 848

Answers (2)

umeshWeb
umeshWeb

Reputation: 53

The way you are expecting is wrong, the test function which you have passed to request.get method is a callback function which will execute in asychronous manner , it means whenever your API responds from the server, that callback function will get execute.

So before that you are expecting the response (body) below the request method, which is wrong.

In this case you just have to write some other function to call this get method and in callback function you can easily access that response or just write your code in that test function itself.

like below - :

request.get({
    url: 'https://api.procore.com/vapid/folders',
    headers: {
        Authorization: "Bearer " + token.access_token
    }, 
    json: {
        company_id: '12594',
        project_id: PID
    }
}, function test(err, response, body){
   // instead of returning body
   // use the body here only
   let result = body;

   // your code here
})

Or the other way -:

const getFolders = function (PID){
var token = getStoredToken();
this.get(function(err, response, body){
    // do whatever you want with the response now
      updateFolder()
})
}

function get(callback){
    request.get({
    url: 'https://api.procore.com/vapid/folders',
    headers: {
        Authorization: "Bearer " + token.access_token
    }, 
    json: {
        company_id: '12594',
        project_id: PID
    }
}, callback)
}

Upvotes: 0

Limboer
Limboer

Reputation: 414

Usually there will be three ways dealing with asynchronous stuff:

  • callback
  • promise
  • async/await

callback:

const getFolders = function(PID, callback) {
  var token = getStoredToken()
  request.get({
    url: 'https://api.procore.com/vapid/folders',
    headers: {
      Authorization: "Bearer " + token.access_token
    },
    json: {
      company_id: '12594',
      project_id: PID
    }
  }, function(err, response, body) {
    callback(body)
  })
}

getFolders(pid, (v) => {
  console.log(v)
})

promise:

const getFolders = function(PID, callback) {
  return new Promise((resolve, reject) => {
    var token = getStoredToken()
    request.get({
      url: 'https://api.procore.com/vapid/folders',
      headers: {
        Authorization: "Bearer " + token.access_token
      },
      json: {
        company_id: '12594',
        project_id: PID
      }
    }, function(err, response, body) {
      if (err) {
        return reject(err)
      }
      resolve(body)
    })
  })
}

getFolders(pid)
  .then(v => {
    console.log(v)
  }).catch(error => {
    console.error(error)
  })

async/await: Due to async/await is actually a syntax sugar, the getFolders function is the same as using promise's, the difference is when you call it:

(async function() {
  try {
    let v = await getFolders(pid)
    console.log(v)
  } catch(e) {
    console.error(e)
  }
})()

Not sure if this solve your confusion.

Upvotes: 2

Related Questions