Skip to content

Instantly share code, notes, and snippets.

@Lcfvs
Last active May 25, 2021 11:02
Show Gist options
  • Save Lcfvs/7a7dcff27a04b0481a0d79fb8cb9f51e to your computer and use it in GitHub Desktop.
Save Lcfvs/7a7dcff27a04b0481a0d79fb8cb9f51e to your computer and use it in GitHub Desktop.
abort controller with progress
function* controller (end) {
let progress = 0
while (1) {
const value = yield progress
if (value === end) {
break
}
progress += value ?? 0
}
return progress
}
const signal = Symbol('controller.signal')
const it = controller(signal)
console.log(it.next()) // { value: 0, done: false } -> get the progress
console.log(it.next(1)) // { value: 1, done: false } -> increase the progress
console.log(it.next(signal)) // { value: 1, done: true } -> get the final progress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment