I'm still getting the hang of working with asynchronous Javascript calls and handling promises. It's a shift from my usual mindset of Do This, Wait for This.
Currently, I'm utilizing localforage on my website and need to retrieve data from two different sources.
var ordersRaw = null
var itemsRaw = null
localforage.getItem('ordersRaw').then((res) => {
if(res) {
ordersRaw = res
}
})
localforage.getItem('itemsRaw').then((res) => {
if(res) {
itemsRaw = res
}
})
if(ordersRaw && itemsRaw) {
// Ready to load the screen
} else { console.log('NO DATA') }
Once I have both sets of data, I want to finish loading the screen by executing a function.
displayWithResults()
The challenge arises when running this code because of its async nature, causing the 'NO DATA' message to appear prematurely.
I find myself constantly facing this dilemma in Javascript and haven't completely mastered it yet. Any insights or tips would be greatly appreciated.