Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

promise #13

Open
xlearns opened this issue Oct 29, 2021 · 0 comments
Open

promise #13

xlearns opened this issue Oct 29, 2021 · 0 comments

Comments

@xlearns
Copy link
Owner

xlearns commented Oct 29, 2021

执行顺序

Promise.resolve().then(() => {
    console.log(0);
    return Promise.resolve(4);
}).then((res) => {
    console.log(res)
})

Promise.resolve().then(() => {
    console.log(1);
}).then(() => {
    console.log(2);
}).then(() => {
    console.log(3);
}).then(() => {
    console.log(5);
}).then(() =>{
    console.log(6);
}) 

雏形

 function Promise(fn){
    this.status = 'pending'
    this.value = null
    this.reason = null
    const success=(data)=>{
      if(this.status=='pending'){
         this.value = data
      }
    }
    const error=(data)=>{
      if(this.status=='pending'){
         this.reason = data
      }
    }
    fn(success,error)
  }
  Promise.prototype.then = function(success,error){
    success(this.value)
    error(this.reason)
  }

  let promise1 = new Promise((resolve, reject) => {
    resolve('data')
    reject('error')
  })
  promise1.then(data => {
    console.log(data)
  },()=>{})

状态完善

异步完善

细节完善

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant