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)
网友评论