Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. const actionsIterator = {
  2. [Symbol.iterator]() {
  3. const steps = this.actions.slice()
  4.  
  5. return {
  6. [Symbol.iterator]() {
  7. return this
  8. },
  9. next(...args) {
  10. if (steps.length > 0) {
  11. let result = steps.shift()(...args)
  12. return { value: result, done: false }
  13. } else {
  14. return { done: true }
  15. }
  16. },
  17. return(v) {
  18. steps.length = 0
  19. return { value: v, done: true }
  20. },
  21. }
  22. },
  23. actions: [],
  24. }
  25.  
  26. // Usage
  27. actionsIterator.actions = [(num) => console.log(num * num)]
  28. const useTask = actionsIterator[Symbol.iterator]()
  29. useTask.next(19)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement