javascript API 4.6 - Error not throwing up in mapView promise callback

722
2
Jump to solution
01-26-2018 06:04 AM
MaximeDemers
Occasional Contributor III

I notice that error that are thrown in the mapView constructor callback are not displayed in the browser console.

For instance:

var view = new MapView({
    "container": "viewDiv",
    "map": map,
    "zoom": 7
}).when(function(view) {    
    throw new Error("This error is not displayed in the browser console");    
    alert("This alert is not executed");
}, function(error) {    
    console.error(error);
});‍‍‍‍‍‍‍‍‍‍‍‍‍

This is really difficult to know where the code is breaking. Is there a solution for this?

Tags (1)
1 Solution

Accepted Solutions
YannCabon
Esri Contributor

Hi,

This is a normal behavior. Here is an snippet using JS Promise that reproduce the same behavior:

Promise.resolve().then(
  () => {
    throw new Error("throwing in the then() callback")
  },
  (error) => { 
    console.error("not catching the error", error);
  }
)
.catch(error => {
  console.error("catching the error", error);
});

You will see that the error callback won't react to what's happening in the success callback.

As a good practice, I never use the error callback, only chain with .catch

Promise.resolve().then(
  () => {
    throw new Error("throwing in the then() callback")
  }
)
.catch(error => {
  console.error("catching the error", error);
})

Your code snippet would look like:

      var view = new MapView({
        "container": "viewDiv",
        "map": map,
        "zoom": 7
      });
      
      view.when(function(view) {    
        throw new Error("This error is displayed in the browser console");
      })
      .catch(function(error) {    
        console.error(error);
      });
‍‍‍‍‍‍‍‍‍‍‍‍‍

Also, make sure to first assign view to the MapView instance. 

// DON'T
var view = new MapView().when(); // view is a promise

// DO
var view = new MapView();  // view is a MapView
var promise = view.when();

View solution in original post

2 Replies
YannCabon
Esri Contributor

Hi,

This is a normal behavior. Here is an snippet using JS Promise that reproduce the same behavior:

Promise.resolve().then(
  () => {
    throw new Error("throwing in the then() callback")
  },
  (error) => { 
    console.error("not catching the error", error);
  }
)
.catch(error => {
  console.error("catching the error", error);
});

You will see that the error callback won't react to what's happening in the success callback.

As a good practice, I never use the error callback, only chain with .catch

Promise.resolve().then(
  () => {
    throw new Error("throwing in the then() callback")
  }
)
.catch(error => {
  console.error("catching the error", error);
})

Your code snippet would look like:

      var view = new MapView({
        "container": "viewDiv",
        "map": map,
        "zoom": 7
      });
      
      view.when(function(view) {    
        throw new Error("This error is displayed in the browser console");
      })
      .catch(function(error) {    
        console.error(error);
      });
‍‍‍‍‍‍‍‍‍‍‍‍‍

Also, make sure to first assign view to the MapView instance. 

// DON'T
var view = new MapView().when(); // view is a promise

// DO
var view = new MapView();  // view is a MapView
var promise = view.when();
MaximeDemers
Occasional Contributor III

Ok thank you! I was confuse because the documentation shows example using the .when() errorCallback instead of the normal promise.catch function. When the .when() errorCallback would be called then?

0 Kudos