promise

# promise

# 实现promise.all

Promise.prototype.all = function (promises){
  let results = []
  let promiseCount = 0
  let promisesLength = promises.length

  return new Promise((resolve, reject) => {
    for(let val of promises) {
      Promise.resolve(val).then(function(res){
        promiseCount++;
        results[i] = res
        if(promiseCount === promisesLength) {
          return resolve(results) // 可以不用 return 出去
        }
      }, function (err){
        return reject(err) // 可以不用 return 出去
      })
    }
  })
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 手写Promise

class myPromise{
    constructor(exc){
      //不能相信用户的输入,所以这里要做参数效验
        if(typeof exc !== 'function'){
            throw new TypeError('this is not a function!')
        }
        this.initValue();
        this.initBind();
        exc(this.resolve,this.reject);
    }
    //进行代码的优化&初始化
    initValue(){
    	//记录状态和值的改变
        //初始化值
        this.state = 'pending';  //初始状态
        this.value = null;  //终值
        this.reason = null; //据因
    }
    //绑定 this
    initBind(){
        this.resolve = this.resolve.bind(this);
        this.reject = this.reject.bind(this);
    }
     //成功后的一系列操作(状态的改变,成功回调的执行)
    resolve(value){
        if(this.state === 'pending'){
            this.state = 'resolve'; //状态进行改变
            this.value = value; //执行成功的回调,把终值进行赋值
        }
    }
    reject(reason){
    //失败后的一系列操作(状态的改变,失败回调的执行)
        if(this.state === 'pending'){
            this.state = 'reject'; //状态进行改变
            this.reason = reason; //执行成功的回调,把据因进行赋值
        }
    }
    then(resolve,reject){
    //  参数效验
        if(typeof resolve !== 'function'){
            resolve = function(value){
                return value;
            }
        }
        if(typeof reject !== 'function'){
            reject = function(reason){
                throw reason;
            }
        }
        if(this.state === 'resolve'){
            resolve(this.value);
        }
        if(this.state === 'reject'){
            reject(this.reason);
        }
    }
}
module.exports = myPromise;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

# 测试写的方法

const myPromise = require('./重新实现Promise')

new myPromise((resolve,reject)=>{
    console.log('it is good');
    resolve(1);
}).then(value=>{
    console.log('value',value);
},reason=>{
    console.log('reason',reason);
})
1
2
3
4
5
6
7
8
9
10
最后更新时间: 2022/10/23 10:24:55