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