Select to view content in your preferred language

deferred action for graphic

969
10
10-17-2018 05:04 PM
LefterisKoumis
Frequent Contributor

What is the best way to wait for this graphic to load?

Line 1, 2 and 3 are loaded (See console.log)

But line 6 suggest not yet, everything is null (see console.log)

I tried 

all(features).then(function (results) {

but it didn't work...

Thanks.

console.log(layerSettings.options.geom)
console.log(layerSettings.options.symbol)
console.log(layerSettings.options.attr)
var graphic = new esri.Graphic(layerSettings.options.geom, layerSettings.options.symbol, layerSettings.options.attr);
features.push(graphic)
console.log(features))

0 Kudos
10 Replies
LefterisKoumis
Frequent Contributor

I modified the code to use promise but still line 16 in the console.log shows the i symbol which means that the graphic was still not ready!

console.log(layerSettings.options.geom)
console.log(layerSettings.options.symbol)
console.log(layerSettings.options.attr)
var graphic = new esri.Graphic();
promise1 = graphic.setGeometry(layerSettings.options.geom)
promisesArray.push(promise1)
promise2 = graphic.setAttributes(layerSettings.options.attr)
promisesArray.push(promise2)
promise3 = graphic.setSymbol(layerSettings.options.symbol)
promisesArray.push(promise3)

                    
var handleAllPromises = Promise.all(promisesArray);
handleAllPromises.then(function (values) {
    features.push(graphic)
    console.log(graphic)                        
    console.log("All the promises are resolved", values);
});
handleAllPromises.catch(function (reason) {
     console.log("One of the promises failed with the following reason", reason);
 });‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

   The setGeometry, setAttributes and setSymbol all return a graphic Not a deferrred (promise) so you can not use them as promises like you are attempting to. Your timing issue seems to be that layerSettings.options is not ready when you are attempting to use it. You need to figure out how to wait for this object. How exactly is this object being created?

0 Kudos
LefterisKoumis
Frequent Contributor

Understood. The layerSettings is an object created from reading a local json file. However, it can have a lot of elements depending the polygon that is stored in the file. So, the layerSettings.options needs to be complete before it can be used.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

   So it sounds like you need to put the json reading portion inside a deferred.

0 Kudos
LefterisKoumis
Frequent Contributor

I located the problem in JSON.parse. The console.log still shows the i symbol. Perhaps the sessionsString is too large to be parsed?

sessionsToLoad = JSON.parse(sessionsString);
                    
console.log(sessionsToLoad);
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

   Since the JSON.parse does not return a deferred or any indication when it is done reading you will have to make a loop that checks for the json to be done pasing by looking at the result sessionsToLoad and looking for a particular hasOwnProperty or just add a setTimeout.

0 Kudos
LefterisKoumis
Frequent Contributor

THis is proven harder than I thought. I did just that, added a property at the end and run a timeout function until it reads that last property but still I get the i symbol!

What I don't understand is that JSON.parse() is a synchronous method, meaning that once it's called it will execute fully, before code execution continues. Why the console.log  is not complete?

sessionsToLoad = JSON.parse(sessionsString);
                    
console.log(sessionsToLoad);

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

  Sorry I don't have an answer to that.

0 Kudos
LefterisKoumis
Frequent Contributor

On another website, it was stated that examining objects via console.log happens in an asynchronous manner. That explains it.

0 Kudos