Instantly share code, notes, and snippets.
mark0978
/ AuthorCache.js
Last active
February 21, 2019 16:29
A simple promise based cache for Authors
This file contains 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
class AuthorCache { | |
cache = {}; | |
getAuthor(url) { | |
// Returns a promise for | |
let result = this.cache[url]; | |
if (result) { | |
if (result.then) { | |
// Request is already in flight, just return this promise, we don't need to do anything else | |
console.debug("Request already in flight for ", url); |
This file contains 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
fetchAuthor = url => { | |
authorCache.getAuthor(url).then(response => { | |
this.setState({ | |
author: response | |
}); | |
}); | |
}; |
This file contains 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
fetchAuthor =(url) =>{ | |
fetch(url) | |
.then(res => res.json()) | |
.then((response) =>{ | |
this.setState({ | |
author:response | |
}) | |
}) | |
} |