question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to return stored value in a function.

See original GitHub issue

Hi,

I’m having trouble figuring out how to retrieve a value from a custom function that accesses the store. I keep getting an undefined value despite having access to the value from within the function.

My code:

export function fetchToken() {
  store.get('userToken')
  .then(token => {
    if (token) {
      return token;
    }
  })
}

If I console out the token in the above function it’s clearly there.

But when I try to use the above function elsewhere like so:

let token = fetchToken() I get an undefined.

Clearly, I’m missing something here. Any help at all would be greatly appreciated .

Thank you!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jasonmerinocommented, Feb 14, 2017

@jaredtibs my apologies for the late reply here.

In your example code you posted, if you are going to get the value of the token you need to move your console.log(token) inside the callback function you pass the .then().

function whateverFunctionThisIs {
  let token;
  fetchToken().then((t) => {
    token = t;
    console.log(token); // will be value stored for token
  });
  console.log(token); // undefined because the callback function passed to .then() hasn't been run yet
  //continues on to make fetch request
}

async function fetchToken() {
  let token
  try {
    token = await store.get('userToken');
    return token
  } catch(e) {
    console.error(e)
  }
}

Or! If you wanted to get really fancy with async/await you could do this.

async function whateverFunctionThisIs {
  let token = await fetchToken();
  console.log(token); // will be value stored for token
  //continues on to make fetch request
}

async function fetchToken() {
  let token
  try {
    token = await store.get('userToken');
    return token
  } catch(e) {
    console.error(e)
  }
}
0reactions
jasonmerinocommented, Feb 15, 2017

No problem!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Function return values - Learn web development | MDN
Active learning: our own return value function · First of all, make a local copy of the function-library. · Let's add some useful...
Read more >
Where is the return value of a function stored? - Stack Overflow
Otherwise the function returns by placing the return value on the stack. I think in .NET it's called Name Return Value Optimization.
Read more >
Where are returns stored? - Codecademy
I understand that console.log and return are different in that console.log is for the purpose of debugging and the value is not stored...
Read more >
12.5. Returning a value from a function - Runestone Academy
It's an assignment statement, so evaluate the right-hand side expression 2 * square(toSquare) . · Look up the values of the variables square...
Read more >
2.2 — Function return values (value-returning functions)
First, your function has to indicate what type of value will be returned. This is done by setting the function's return type, which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found