How to return stored value in a function.
See original GitHub issueHi,
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:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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()
.Or! If you wanted to get really fancy with async/await you could do this.
No problem!