Why does my ES6 Promise Rejection need to be destroyed immediately to avoid a console error message?

Please note: The following is a problem that behaves differently in different browsers. Perhaps this is a problem with the implementation of the browser. I would love some advice.

In my application, I create a pair of promises that I may not consume until a certain time in the future. What should be good, they promises, after all.

If the remaining promise is resolved, no problem. I can use it as far as possible in the future as I want, and as many times as I want. As expected.

If the pending promise is rejected, however, there is a problem. If I do not take advantage of this rejection shortly after creating it (not sure how soon), a console message will appear in the Chrome or Firefox browser, indicating that there is a fuzzy promise of rejection / error. IE does not cause this error.

So, consider the following code:

console.log("About to make a promise rejection.");
var foo = Promise.reject(new Error("Foo Rejected Promise."));
console.log("Promise rejection made.");

Please note that there is no benefit or consumption of the promise foo. It is just kept for future use.

The console in IE looks like this:

About making promises.

The promised rejection.

Expected. However, the same code in Chrome will give the following console:

About making promises.

The promised rejection.

Unprepared (in promise) Error: Foo The promise was rejected. (...)

Firefox looks just like Chrome, except for the wording around the "uncaught" error.

, "" , , . ..... , .

" " , :

console.log("About to make a promise rejection.");
var foo = Promise.reject(new Error("Foo Rejected Promise."));
console.log("Promise rejection made.");
setTimeout(() => {
    foo.catch((err) => {
        console.log("Displaying " + err.message + " 10 seconds later.");
    });
}, 10000);

"" , - . IE , :

, .

.

Foo . 10 .

Firefox IE .

Chrome :

, .

.

( ) : Foo . (...)

Foo . 10 .

, Chrome , , , .

, , . "" , , :

console.log("About to make a promise rejection.");
var foo = Promise.reject(new Error("Foo Rejected Promise."));
foo.catch(() => { });  // Added this hack-ish code.
console.log("Promise rejection made.");
    setTimeout(() => {
    foo.catch((err) => {
        console.log("Displaying " + err.message + " 10 seconds later.");
    });
}, 10000);

.

: , Chrome ( - FireFox), ? . -, , , ?

.

+4
2

, , , .

, . , "hack-ish" .catch(() => { }).

Chrome

var foo = Promise.reject(new Error("Foo Rejected Promise."));
foo.catch(() => { });  // Added this hack-ish code.

setTimeout(() => {
    foo.catch((err) => {
        console.log("Displaying " + err.message + " 10 seconds later.");
    });
}, 10000);

,

try {
  throw new Error("Foo"));

  setTimeout(() => {
    try {
      throw new Error("Foo"));
    } catch (err) {
      console.log("Displaying " + err.message + " 10 seconds later.");
    }
  }, 10000);
} catch (err) {}

10 , try...catch , ? . "hack-ish" try { ... } catch (err) {}.

Bluebird . Bluebird promises .

Angular 2, promises Zone.js.

, promises :

if (typeof DEBUG_PROMISE === 'undefined') {
  window.addEventListener('unhandledrejection', (e) => {
    e.preventDefault();
    console.warn(e.reason);
  });
}

.

+3

-, promises , , Chrome. , .then(), , . .

function throwException(c){ return a.b = 2 ;} //a is not defined. throws exception.
let p = q.resolve("a")
         .then(x => x + "b")
         .then(y => throwException(y))
         .then(z=> z+"c");   //this will say nothing in Q library 

, .

Q .done() , Chrome:

let p = q.resolve("a")
         .then(x => x + "b")
         .then(y => throwException(y))
         .then(z=> z+"c")
         .done();   //this will throw an exception.

, promises. Chrome .done() , .catch(_=>{}), promises. Q , .done(), . , . .

+2

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


All Articles