Skip to content

Instantly share code, notes, and snippets.

@cantremember
Last active September 3, 2017 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cantremember/44b9bdee9deb817a5180 to your computer and use it in GitHub Desktop.
Save cantremember/44b9bdee9deb817a5180 to your computer and use it in GitHub Desktop.
Promises: Early Return
// 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