In a node.js application I need to do something like this: (pseudocode, of course)
if(loadedData is empty){
loadDatafromDatabase <<---- this one is async
}
useLoadedData
Of course, loading data from database is an async process, so useLoadedData tries to use data BEFORE the loading is complete. Is there any clean way to wait for loadDataFromDatabase to return its results, before going on?
I've seen many people mentioning callbacks as a correct solution, so I was tempted to do something like:
if(loadedData is empty){
loadDataFromDatabase( callback useLoadedData )
}else{
useLoadedData
}
but it looks quite dirty to me. Any better approach?
Thanks!
useLoadedData()once elsewhere and call it/pass it as a callback in your code as many times as you wantif(loadedData is empty) {loadData; useData;} else{useData;}