今天作了海康威视的笔试题,感受不是很妙...但仍是作个 记录分享一下,这道题仍是作对了的。函数
function red(){
console.log('red');
}
function green(){
console.log('green');
}
function yellow(){
console.log('yellow');
}
复制代码
题目要求用Promise来完成,那么,就写起来吧。 先写一个Promise函数spa
var light = function(timmer, cb){
return new Promise(function(resolve, reject) {
setTimeout(function() {
cb();
resolve();
}, timmer);
});
};
var step = function() {
Promise.resolve().then(function(){
return light(3000, red);
}).then(function(){
return light(2000, green);
}).then(function(){
return light(1000, yellow);
}).then(function(){
step();
});
}
step();复制代码