tingying

1 addEventListener()第三个参数

这个参数设计到事件的捕获与冒泡,为true时捕获,false时冒泡。

冒泡:从里面往外面触发事件

捕获:从外面往里面触发事件

要想冒泡,就要将每个监听事件的第三个参数设置为false,也就是默认的值。

要想捕获,就要将每个监听事件的第三个参数设置为true。

 

2 手写代码实现promise.all

1 首先看promise.all的使用

const p1 = new Promise((resolve, reject) => {
  setTimeout(() => {
      resolve(111);
  }, 1000);
});
const p2 = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve(222);
    }, 2000);
});

Promise.all([p1, p2]).then(res => {
    console.log(res);
})

2 分析-promise.all能使用then, 说明它返回一个promise。接收一个promise数组

function myPromiseAll(list) {
    return new Promise((resolve, reject) => {
        let arr = [];
        let num = 0;
        list.forEach(item => {
            item.then(res => {
                arr.push(res);
                num++;
                if(num === list.length) {
                    resolve(arr);
                }
            })
        });
    })
}
myPromiseAll([p1,p2]).then(res => {
    console.log(res);
})

 

分类:

技术点:

相关文章:

  • 2021-09-30
  • 2021-09-30
  • 2021-09-30
  • 2021-09-30
  • 2021-09-30
猜你喜欢
  • 2021-09-30
  • 2021-11-09
  • 2021-04-18
相关资源
相似解决方案