3

I've been reading up on promises for a few hours and I'm loosing my mind here. According to what I've read in SO and articles on google. To get the value of a response in a variable I have to use async and await. So going that route I've written this code:

async function getZpid() {

let response = await function getZillowZpid(){
zillow.get('GetSearchResults', parameters)
    .then(results => {
      let zpidResponse = results.response.results.result[0].zpid;
      console.log("i ran");
      return zpidResponse;
  });
}

}

getZpid();

Here I would expect that when getZpid() runs "response" would wait for the function get ZillowZpid to be done. Once it would be completed it would console.log "i ran" and then return the zpidResponse.

I get no error but the console.log never appears. Ultimately the console.log is just a test what I am trying to do is get zpidResponse in to a variable I could use outside the function. Any help would be greatly appreciated!

1 Answer 1

3

You are defining an extraneous function with await function getZillowZpid() which you never call. This is why you see no error or results. If you log the variable response in your function above you will see that it is something [Function: getZillowZpid], you've defined a function and assigned it to response which is certainly not what you want.

Since zillow.get returns a promise, you can just await that. Here's an example with a mocked zillow object:

// fake the zillow object
let zillow = {
  get() {
    return Promise.resolve({response: {results: {result: [{zpid: "Some data"}]}}})
  }
}

async function getZpid() {
  let parameters = {}
  // zillow.get returns a promise, you can use await to get the returned value
  let response = await zillow.get('GetSearchResults', parameters)
      .then(results => {
        let zpidResponse = results.response.results.result[0].zpid;
        console.log("i ran");
        return zpidResponse;
    });
  
  console.log(response)
  }
  
  getZpid();
  

FYI, if you're using async/await it's cleaner to avoid the then() although you still should add some error-checking:

// fake zillow
let zillow = {
  get() {
    return Promise.resolve({response: {results: {result: [{zpid: "Some data"}]}}})
  }
}
async function getZpid() {
  let parameters = 0
  let response = await zillow.get('GetSearchResults', parameters)
  let zpidResponse = response.response.results.result[0].zpid;
  console.log(zpidResponse)
  return zpidResponse
  }

  getZpid();
  

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

2 Comments

With this it returns Promise { <pending> } any way to get the actual value outside of the function? @Mark Meyer
Hi @FabricioG async functions always return promises. So fro the caller to get the return value you need to call getZpid().then(val => /* use value */)

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.