2018-06-13

2018-06-13  本文已影响0人  白夜的记事本

http://www.touchui.io/

promise

https://segmentfault.com/a/1190000002928371 基础入门

https://zhuanlan.zhihu.com/p/26523836

https://zhuanlan.zhihu.com/p/29632791

https://zhuanlan.zhihu.com/p/32897019

https://zhuanlan.zhihu.com/p/26767436

https://zhuanlan.zhihu.com/p/30797777 经典promise 案例题

题目:红灯三秒亮一次,绿灯一秒亮一次,黄灯2秒亮一次;如何让三个灯不断交替重复亮灯?(用Promse实现)

function red() {

  console.log('red');

}

function green() {

  console.log('green');

}

function yellow() {

  console.log('yellow');

}

let light = (fn, timer) => new Promise(resolve => {

  setTimeout(function() {

    fn()

    resolve()

  }, timer)

})

// times为交替次数

function start(times) {

  if (!times) {

    return

  }

  times--

  Promise.resolve()

    .then(() => light(red, 3000))

    .then(() => light(green, 1000))

    .then(() => light(yellow, 2000))

    .then(() => start(times))

}

start(3)

上一篇 下一篇

猜你喜欢

热点阅读