AnsweredAssumed Answered

How to properly check if a FeatureLayer object has been loaded?

Question asked by Eletrosul on Apr 27, 2018
Latest reply on May 4, 2018 by odoe

So, I know the documented way to do it would be like this :

 

const fl = new featureLayer(url);
fl.on('load', () => {
   doSomething();
})

 

 

But there is a problem with the approach above, if the featureLayer has already loaded between its creation and the event binding, doSomething() will never trigger.

 

This would be solved by simply having a callback parameter on the featureLayer constructor, which doesn't.

 

So, for now I'm using the following solution :

 

const fl = new featureLayer(url);
if(!fl.loaded){            
    fl.on('load', () => {                   
        doSomething();               
    })
}else{               
    doSomething();
}

 

 

But there's also a very nasty possible error with my temporary solution, it can happen that the featureLayer will load between the if/else check, and then doSomething() will never trigger.

 

Is there an actual solution to this? Is this a bug? Am I missing something?

 

Here's a pen showing the issue : FeatureLayer 

I'm using the setTimeout to force the layer to load, in our company the arcgis server is local so they load really fast, before binding the event as I stated above

Outcomes