SOURCE

// //1.实现pormise顺序执行
// const arr = [1, 2, 3, 4]
// //使用reduce 本质上就是不断地叠加then 每次执行操作完都来一个then 等上面执行完再执行下面的
// arr.reduce((pre, cur) => pre.then(
// 	() => new Promise(
//     	(resolve) => setTimeout(() => resolve(console.log(cur)), 1000)
//     )

// ), Promise.resolve())
//方法2
// let time = 1;
// arr.forEach( item => Promise.resolve(item)
// .then(() => new Promise(
//     resolve => setTimeout(() => resolve(console.log(item)), 1000 * time++)
// )))
//实现红绿灯交替 红灯三秒亮一次,黄灯两秒亮一次,绿灯一秒亮一次
function red() {
    console.log('red')
}
function yellow() {
    console.log('yellow')
}
function green() {
    console.log('green')
}
function light(cb, timer) {
    return new Promise( resolve => {
        setTimeout(() => {
            cb();
            resolve()
        }, timer)
    })
}
function step() {
   Promise.resolve().then(() => light(red, 3000))
   .then(() => light(yellow, 2000))
   .then(() => light(green, 1000))
   .then(() => step())
}
step()
//封装一个加载图片的方法
function loadImg(url) {
  return new Promise((resolve, reject) => {
    const img = new Image();
    img.onload = function() {
      console.log("一张图片加载完成");
      resolve(img);
    };
    img.onerror = function() {
    	reject(new Error('Could not load image at' + url));
    };
    img.src = url;
  })
}
console 命令行工具 X clear

                    
>
console