0

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!

2
  • 2
    Does this answer your question? How do I return the response from an asynchronous call? Commented Jan 21, 2020 at 17:09
  • 1
    return.... why? Just do what you need to do with that data in your callback (e.g. call whatever function knows how to work with it there)? Also, making getFolders call getFolders sounds like a really bad idea. Commented Jan 21, 2020 at 17:10

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.