kriskowal / q

A promise library for JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Catch exceptions in Q promises with settimeout

ionescudev opened this issue · comments

I have the following code:

function getData(){
    var prr = Q.defer();

setTimeout(function(){
 prr.resolve();
},200)

    return prr.promise;
}

function promisedStep1(){
    var def = Q.defer();

    getData().then(function(){
        // ww does not exist hence it'll throw an exception
        // this exception will be swalloed and done wont run.
        var  result = doesnotexist / 3;
        def.resolve();
    })
    

    return def.promise;
 }

    function promisedStep2(){
        var def = Q.defer();

    setTimeout(() => {
        def.resolve();
    }, (200));

    return def.promise;
}

    Q('init').then(promisedStep1)
.then(promisedStep2, function(){
    console.log("error");
})

.catch(function (error) {
    console.log(error);
})
.done(function(){
    console.log(arguments);
},function(){
    console.log(arguments);
});



I would like somewhere in the console that my exception regard  var  result = doesnotexist / 3;
be printed, i do no want to wrap anything in try catch.

Where is the global exception handler in Q?


Edit, i tried bluebird promise and it seems to work great, it catches my exception and prints where it happened.

Why doesnt Q work? my current job collegues all use Q library and wont switch to bluebird, is there a way to make Q work as bluebird? thank you.

Why doesnt Q work? my current job collegues all use Q library and wont switch to bluebird, is there a way to make Q work as bluebird? thank you.

As a maintainer of both bluebird and Q, I can assure you that bluebird does not intercept requests thrown in a setTimeout and never has. Promises work by chaining and aren't magic.