Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled rejection reasons (should be empty)

I'm getting into promises pattern with Q and I keep getting warning "[Q] Unhandled rejection reasons (should be empty)" in console. What em I doing wrong?

http://jsfiddle.net/FpyDr/1/

function load(url) {
    var deferred = Q.defer();

    $.ajax({
        type: "GET",
        processData: false,
        dataType: "html",
        url: url,
        cache: false
    }).done(function (response, status, xhr) {

        deferred.reject(new Error("test error"));

        return;
    }).fail(function (xhr, status, error) {

        deferred.reject(new Error("ajax failed"));

        return;
    });

    return deferred.promise;
}

load("http://fiddle.jshell.net")
    .then(function (result) {
        console.log("got result", typeof(result));
    })
    .catch(function (error) {
        console.log("got error", error);
        return true;
    })
    .done();
like image 862
Lukáš Novotný Avatar asked Jul 09 '13 09:07

Lukáš Novotný


People also ask

How do you deal with unhandled rejection?

If an error condition arises inside a promise, you “reject” the promise by calling the reject() function with an error. To handle a promise rejection, you pass a callback to the catch() function. This is a simple example, so catching the rejection is trivial.

What is unhandled Promise rejection in JavaScript?

The Promise. reject() method returns a Promise object that is rejected with a given reason. The unhandledrejection event is sent to the global scope of a script when a JavaScript Promise that has no rejection handler is rejected; typically, this is the window, but may also be a Worker.


1 Answers

Based on this confusing discussion it's a false positive.

To silence the logging you can do this:

Q.stopUnhandledRejectionTracking();

If you didn't capture the rejection it would throw the error, so you'll still see it in the console after adding the code above. JSFiddle: http://jsfiddle.net/homeyer/FpyDr/22/

like image 88
Andrew Homeyer Avatar answered Sep 21 '22 11:09

Andrew Homeyer