Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const actionsIterator = {
- [Symbol.iterator]() {
- const steps = this.actions.slice()
- return {
- [Symbol.iterator]() {
- return this
- },
- next(...args) {
- if (steps.length > 0) {
- let result = steps.shift()(...args)
- return { value: result, done: false }
- } else {
- return { done: true }
- }
- },
- return(v) {
- steps.length = 0
- return { value: v, done: true }
- },
- }
- },
- actions: [],
- }
- // Usage
- actionsIterator.actions = [(num) => console.log(num * num)]
- const useTask = actionsIterator[Symbol.iterator]()
- useTask.next(19)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement