Promise rejecting in function

In javascript how can I reject a promise? Simply returning new Error('blah') doesnt work right error console throws ".then is not a function" or something like that.

So I included Promise.jsm and then did return Promise.reject(new Error('blah')) so my question is: if im in a promise like: then i dont have to return Promise.rject and can just return new Error right?

 //mainPromise chains to promise0 and chains to promise1
function doPromise(rejectRightAway) {
    if (rejectRightAway) {
        return Promise.reject(new Error('intending to throw mainPromise rejection')); //is this right place to use Promise.reject? //returning just new Error throws in error console, something like: '.then is not a function' and points to the onReject function of mainPromise.then
    }

    promise0.then(
        function () {

            var promise1 = somePromiseObject;
            promise1.then(
                function () {
                    alert('promise1 success');
                },
                function (aRejReason) {
                    alert('promise1 rejected ' + aRejReason.message);
                    return new Error('promise1 rejected so through promise0 rej then mainPromise rej'); //want to throw promise1 rejection //ok to just return new Error?
                }
            );
        },
        function (aRejReason) {
            alert('promise0 rejected with reason = ' + aRejReason.message);
        }
    );

    return promise0;

}

var mainPromise = doPromise();
mainPromise.then(
    function () {
        alert('mainPromise success');
    },
    function (aRejectReason) {
        alert('mainPromise rejected with reason = ' + aRejectReason.message)
    }
);

im not sure if this code works, so my question is how do i reject --- like when is it right to return Promise.reject(new Error()) and when to just new Error()?

Answers:

Answer

tl:dr

  • In order to indicate normal completion of an operation, you return its result return result.
  • In order to indicate an exception, you throw new Error(reason) just like in synchronous code, promises are throw safe.
  • It is possible to explicitly return rejections from chains, but often unneeded.

One of the great things about promises is that they fix exception handling in asynchronous code.

Promises are not just a < buzzwords> composing sequential monadic DSL < /buzzwords> abstraction over concurrency - they are also throw safe!

Throw safety is really awesome in this regard, you can act just like ou would in sequential code:

function foo(){
    if(somethingWrong){
        // instead of returning normally, we throw, using the built in exception chain
        // code blocks have. This indicates something unexpected breaking the sequence of cour code
        throw new Error("Something was wrong");
    }
    return "some normal result"; // here we indicate a function terminated normally,
                                 // and with which value.
}

Callbacks in node use the (err,result...) convention which I find ugly. With promises, You gain the benefits of error handling you have in sequential code again:

somePromise.then(function(){
     if(rejectRightAway){
          throw new Error("rejecting right away...");
     }
     return "some normal result";
})....

See how similar it is to sequential code? Whenever you want to reject you throw from the then handler, whenever you want to fulfill you return from the then handler. Just like in "synchronous" code.

So in your case:

//want to throw promise1 rejection //ok to just return new Error?
return new Error('promise1 rejected so through promise0 rej then mainPromise rej'); 

Is solved exactly with what you intuitively wanted to do:

 throw new Error("promise1 rejected so through promise0 rej then mainPromise rej");
Answer

You'll need to reject the specific promise:

return promise0.reject(new Error('intending to throw mainPromise rejection'));

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.