Skip to content

Instantly share code, notes, and snippets.

@jonathonherbert
Created August 11, 2020 14:18
Show Gist options
  • Save jonathonherbert/4fe5729c2846e1423c9973470f7386fb to your computer and use it in GitHub Desktop.
Save jonathonherbert/4fe5729c2846e1423c9973470f7386fb to your computer and use it in GitHub Desktop.
Demo of closure behaviour
"use strict";
class ExampleAsyncService {
currentSearch = undefined;
search(searchTerms) {
this.currentSearch = searchTerms;
console.log(`Searching for ${searchTerms}`);
setTimeout(() => {
if (this.currentSearch !== searchTerms) {
console.log(`Search for ${searchTerms} superceded, aborting`);
} else {
console.log(`Search for ${searchTerms} complete`);
}
}, 500);
}
}
const service = new ExampleAsyncService();
service.search("search1");
service.search("search2");
setTimeout(() => service.search("search3"), 600);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment