javascript - Why does Promise.reject() require a return? -
in following code promise.reject
doesn't work unless use return promise.reject(...)
. why this?
promise.resolve('promise 1 done') .then(function(result) { console.log(result); return 'promise 2 done' }).then(function(result) { let j; try { j = json.parse("invalid throw"); console.log(j); } catch(err) { promise.reject('could not parse json'); } console.log(result); }).catch(function(err) { console.log(err); });
promise.reject
creates value, not throw exception breaks function throw
does. if don't return
value, ignored , control flow continues.
given inside promise callback, (and possibly should) instead use
throw new error('could not parse json');
Comments
Post a Comment