Skip to content

Instantly share code, notes, and snippets.

@arikfr
Created July 13, 2016 10:28
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 arikfr/8be1aa64203aca7ca4f3bb9c2fb9e8d0 to your computer and use it in GitHub Desktop.
Save arikfr/8be1aa64203aca7ca4f3bb9c2fb9e8d0 to your computer and use it in GitHub Desktop.
Promises Caching
var inProgressUrls = {};
function grabScreenshot(url) {
var inProgress = inProgressUrls[url];
if (inProgress) {
return inProgress;
}
var promise = new Promise(function(fulfill, reject) {
... grab screenshot ...
// Once done, remove the Promise from the map.
delete inProgressUrls[url];
});
inProgressUrls[url] = promise;
return promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment