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()
?