Javascript Promise: Why is the caught exception still written as unmapped?

When I create my promise and call the failure function, the error must be caught by the .catch function to promise correctly? But in console.log, I still get it as non-displayable (the .catch function also runs). What for? Or is it intended? I think that I have something conceptually wrong and I want to be enlightened!

Consider the following example:

var A = {
  loadingPromise: null,
  loadingPromiseFail: null,
  loadingPromiseResolver: null,

  init: function() {
    this.loadingPromise = new Promise(
      function(resolve, fail) {
        this.loadingPromiseResolver = resolve;
        this.loadingPromiseFail = fail;
      }.bind(this)
    );

    this.loadingPromise.then(function(data) {
      console.log('success');
    }.bind(this));

    this.loadingPromise['catch'](function(e, x) {     
      console.log('error', e);
    }.bind(this));
  },

  doSomething: function() {
    setTimeout(function(){ 
     this.loadingPromiseFail('404');
    }.bind(this), 1000);
  }

}

A.init();
A.doSomething();

console.log:

error 404
uncaught exception: 404

Why the second?

Also here: https://jsfiddle.net/Paflow/4g7yj38b/6/

+4
source share
1 answer

This bit of code

this.loadingPromise.then(function(data) {
  console.log('success');
}.bind(this));

doesn't have a catch, so the error is really not caught

Promises

var A = {
  loadingPromise: null,
  loadingPromiseFail: null,
  loadingPromiseResolver: null,

  init: function() {
    this.loadingPromise = new Promise(
      function(resolve, fail) {
        this.loadingPromiseResolver = resolve;
        this.loadingPromiseFail = fail;
      }.bind(this)
    );

    this.loadingPromise.then(function(data) {
      console.log('success');
    }.bind(this)).catch(function(e, x) {
      console.log('error', e);
    }.bind(this));
  },

  doSomething: function() {
    setTimeout(function() {
      this.loadingPromiseFail('404');
    }.bind(this), 1000);
  }

}

A.init();
A.doSomething();

https://jsfiddle.net/4g7yj38b/7/

, .then( .catch) Promise... "" , .catch "", , .

+6

Source: https://habr.com/ru/post/1661698/


All Articles