Reputation: 204
React Native Development:
I am in the process of building a react native application and I'm learning the basics needed to understand promises. I am finding it difficult to work out why some things are happening. My biggest issue so far is that the value I return from a promise is not accessible to me. Here is my function and here is the console log:
Function:
getUri(){
var data = this.apiclient.retrieveImages()
var outside_uri = data.then((uri) => { console.log('inside => ' + uri); return uri;})
console.log('outside => ' + outside_uri)
}
Console log:
outside => [object Object]
inside => https://firebasestorage.googleapis.com/v0/b/facelapse-50017.appspot.com/o/Q…M0p7x1ROK0yIN02%2F001?alt=media&token=31de9def-cd32-4d5c-ad65-f3f65ad76685
A quick observation: The outside is printing before the inside (possibly because of the async nature of a promise).
Upvotes: 1
Views: 2784
Reputation: 204
Solution: By making my entire function asyncronous and making the outside_uri await the execution of the promise, the outside was no longer being evaluated before the promise was completed.
async getUri(){
var data = this.apiclient.retrieveImages()
var outside_uri = await data.then((uri) => { console.log('inside => ' + uri); return uri;})
console.log('outside => ' + outside_uri)
}
Upvotes: 3