I'm relatively new to Javascript, and I'm a bit confused about promises and their scope.
Let's say I have the following method:
function fetchAllData(){
//Calling backend to fetch data, returns a promise
backend.getMoreData(localData.length, 1000).then(function(data){
//Store fetched data
for(let i = 0; i < data.length; i++){
localData.push(data[i]);
}
//If there's more data, invoke fetchData() again
if(data.length > 0){
log("Fetching more data...");
fetchAllData();
} else{
log("Fetched all data!");
}
} );
}
Basically the method operates like this:
- Fetches some data from a backend API (the API returns a promise)
- Once the promise is fulfilled, the data is added to a local variable
- If there's more data to fetch, the function is called recursively to fetch more, until all data has been fetched
My question is: is this a potential "stack bomb"? Or, thanks to the promise mechanism, the invoking function is popped from the stack before the "then()" method is invoked?
I ask because I'm seeing highest than expected memory usage and some browser crashes when using this, almost making me suspect the various instances of data
are not being de-allocated until the entire chain has finished