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?
Solved! Go to Solution.
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();
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();
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?