Assetmanagner onsuccess

I am using asset manager to load multiple objects, like this:

const task1 = assetsManager.addMeshTask....
task1.onSuccess = callback1
const task2 = assetsManager.addMeshTask....
task1.onSuccess = callback2
const task3 = assetsManager.addMeshTask....
task1.onSuccess = callback3

I want to run callback3 only if callbak1 and callback2 are completed, to check that I am saving a state like this:

var callback1Completed = false, callback2Completed = false,
const task1 = assetsManager.addMeshTask....
task1.onSuccess = callback1( //when all task completed set callback1Completed = true )
const task2 = assetsManager.addMeshTask....
task2.onSuccess =  callback2( //when all task completed set callback2Completed = true )

so in my last task I am checking with an interval if above 2 tasks are completed like:

const task3 = assetsManager.addMeshTask....
task3.onSuccess = callback3 (){
var interval = setInterval(() => {
    if(callback1Completed && callback2Completed){
    clearInterval(interval) ;
    // do my stuff
   }
}, 1000);
}

I know we can use assetsManager.onProgress and assetsManager.onFinish, But they do not take in account onSuccess callback function? I would like to know when onSuccess is finished, is there any efficient way in Babylon, without use of states and interval, like I am doing above?

Hi @Dshah_H,

This is a more general problem of handling async javascript functions. You can probably do something like this to avoid setInterval. Hope you get the idea. To write async calls in a more readable way, you can consider promises.

let callback1Completed, callback2Completed, task3Completed;
// callback functions
const cb1 = () => {
     ...
     callback1Completed = true;
     cb3_onlyAfter1And2();
};
const cb2 = () => {
     ...
     callback2Completed = true;
     cb3_onlyAfter1And2();
};
const cb3 = () => {
     task3Completed = true;
     cb3_onlyAfter1And2();
}; 
const cb3_onlyAfter1And2 = () => {
     if (callback1Completed && callback2Completed && task3Completed) {
         ...
     }
};

const task1 = assetsManager.addMeshTask....
task1.onSuccess = cb1;
const task2 = assetsManager.addMeshTask....
task2.onSuccess =  cb2;
const task3 = assetsManager.addMeshTask....
task3.onSuccess =  cb3;

Thanks @slin
promises I would like to avoid as much I would setInterval, I gave a small example above for state management using variable, but in my real app I am using sockets events, I was looking if BabylonJS supports such event management.