Skip to content

Instantly share code, notes, and snippets.

@seriousManual
Created July 25, 2019 09:45
Show Gist options
  • Save seriousManual/6aa5bc4ed9fe2cfefc786841482ded7a to your computer and use it in GitHub Desktop.
Save seriousManual/6aa5bc4ed9fe2cfefc786841482ded7a to your computer and use it in GitHub Desktop.
Request synchronization
<html>
<head>
<script>
class Service {
inProgress = null;
async uploadData() {
if (!this.inProgress) {
console.log('ohay, I\'m actually doing a request');
this.inProgress = new Promise(resolve => {
// simulate a request by resolving after 3s
setTimeout(() => {
this.inProgress = null;
resolve(Math.random());
}, 3000);
});
}
return this.inProgress
}
}
const service = new Service();
(async function () {
const results = await Promise.all([
service.uploadData(),
service.uploadData(),
service.uploadData(),
service.uploadData(),
]);
console.log(results);
console.log(await service.uploadData());
console.log(await service.uploadData());
})();
</script>
</head>
<body>
<h1>sync</h1>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment