Monday, 10 April 2017

Promise do not bubble outside

I need to test function testMe using Mocha. But there is trouble when my unit test throw an error. Here is simpified example

function testMe(callback) {
    new Promise((resolve, reject) => {
        setTimeout(() => resolve([1,2,3]), 1000);
    }).then((result) => {
        callback(null, result);
    }).catch((error) => {
        callback(error, null)
    });
}

testMe((err, result) => {
   if(err) throw new Error();
   if(result.length < 5) throw new Error();
});

In this example after throw runs catch block. But I need to run catch block only after reject.



via Игорь

No comments:

Post a Comment