Skip to content

Instantly share code, notes, and snippets.

View mark0978's full-sized avatar
🏠
Working from home

Mark Jones mark0978

🏠
Working from home
View GitHub Profile
@mark0978
mark0978 / AuthorCache.js
Last active February 21, 2019 16:29
A simple promise based cache for Authors
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);
@mark0978
mark0978 / Author.js
Created February 21, 2019 15:36
The Cached version of the Async fetching of an author
fetchAuthor = url => {
authorCache.getAuthor(url).then(response => {
this.setState({
author: response
});
});
};
@mark0978
mark0978 / Author.js
Last active February 21, 2019 15:34
Async fetching of author data before caching
fetchAuthor =(url) =>{
fetch(url)
.then(res => res.json())
.then((response) =>{
this.setState({
author:response
})
})
}