Guest User

Untitled

a guest
Feb 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.36 KB | None | 0 0
  1. //Promise基础原理
  2.  
  3. class P {
  4. constructor (fn) {
  5. this.resoleveCbs = []
  6. this.resoleveFn = () => this.resoleveCbs.forEach(cb => cb())
  7. fn(this.resoleveFn)
  8. }
  9.  
  10. then (cb) {
  11. this.resoleveCbs.push(cb)
  12. }
  13. }
  14.  
  15.  
  16. console.log('start')
  17.  
  18. let p = new P(resolve => {
  19. setTimeout(() => {
  20. resolve()
  21. }, 1000)
  22. })
  23.  
  24. p.then(() => {
  25. console.log('resolve')
  26. })
Add Comment
Please, Sign In to add comment