Quantcast
Channel: Comments on Promises and Static Values
Viewing all articles
Browse latest Browse all 9

By: Gordon Smith

$
0
0

Also that cache “pattern” has a race condition if its called twice in quick succession (it may end up calling the server twice) as there is a window of opportunity during the “await” for the server call.

The pattern I tend use roughly (untested code) like this:

const cache = {
    /* url: content */
};

// Async function that returns cached content or retrieves fresh content
async function getInfo(url) {
    // Check for value in cache
    if (!cache[url]) {
        cache[url] = fetch("https://www.facebook.com").then(r => r.text());
    }
    return cache[url];
}

There isn’t any real need for an “await” inside the function…


Viewing all articles
Browse latest Browse all 9

Latest Images

Trending Articles



Latest Images