Skip to content

Instantly share code, notes, and snippets.

@qls0ulp
Forked from domenic/not-bad-code.js
Created July 4, 2020 09:07
Show Gist options
  • Save qls0ulp/18eea6db21d43386bafdb44d8ddd8758 to your computer and use it in GitHub Desktop.
Save qls0ulp/18eea6db21d43386bafdb44d8ddd8758 to your computer and use it in GitHub Desktop.
Avoiding explicit promise construction antipattern
function getUserDetail(username) {
if (userCache[username]) {
return Promise.resolve(userCache[username]);
}
// Use the fetch API to get the information
return fetch('users/' + username + '.json')
.then(function(result) {
userCache[username] = result;
return result;
})
.catch(function() {
throw new Error('Could not find user: ' + username);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment