Last active
September 3, 2017 06:13
-
-
Save cantremember/98224602f7cb9e78f235 to your computer and use it in GitHub Desktop.
Promises: Throw or Reject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* be careful to return a Promise from every exiting branch | |
*/ | |
function mayReject(may) { | |
if (may || (may === undefined)) { | |
return Promise.reject(new Error('I REJECT')); | |
} | |
return Promise.resolve(true); | |
} | |
/** | |
* or add a candy-coated Promise shell, | |
* and write it more like you're used to | |
*/ | |
var mayThrow = Promise.method(function(may) { | |
if (may || (may === undefined)) { | |
throw new Error('I REJECT'); | |
} | |
return true; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment