Last active
May 25, 2021 11:02
-
-
Save Lcfvs/7a7dcff27a04b0481a0d79fb8cb9f51e to your computer and use it in GitHub Desktop.
abort controller with progress
This file contains hidden or 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
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