Created
August 11, 2020 14:18
-
-
Save jonathonherbert/4fe5729c2846e1423c9973470f7386fb to your computer and use it in GitHub Desktop.
Demo of closure behaviour
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
"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