Last active
September 3, 2017 06:13
-
-
Save cantremember/44b9bdee9deb817a5180 to your computer and use it in GitHub Desktop.
Promises: Early Return
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
// const ... | |
var EARLY_RETURN; | |
function _getSomething(key, data) { | |
var scopedResult; | |
return cache.fetchSomething(key) | |
.then(function(_resolved) { | |
scopedResult = _resolved; | |
if (scopedResult) { | |
// no need to create what we can fetch | |
throw EARLY_RETURN; | |
} | |
return _createSomething(data); | |
}) | |
// ... | |
.then(function(_resolved) { | |
scopedResult = _resolved; | |
return cache.putSomething(key, _resolved); | |
}) | |
.catch(function(err) { | |
if (err !== EARLY_RETURN) { | |
// oh, that's an Error fo real | |
throw err; | |
} | |
}) | |
.then(function() { | |
return scopedResult; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment