Created
July 25, 2019 09:45
-
-
Save seriousManual/6aa5bc4ed9fe2cfefc786841482ded7a to your computer and use it in GitHub Desktop.
Request synchronization
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
<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