const arr = [1, 2, 3];
arr.reduce((p,x) =>{
return p.then(() => {
return new Promise(r => {
setTimeout(() => r(console.log(x)), 1000)
})
})
}, Promise.resolve());
function red() {
console.log('red');
}
function green() {
console.log('green');
}
function yellow() {
console.log('yellow');
}
const light = function(tiemer, cb){
return new Promise(resolve => {
setTimeout(() => {
cb();
resolve();
}, tiemer);
})
}
const step = function() {
Promise.resolve().then(() => {
return light(3000, red);
}).then(() => {
return light(2000, green);
}).then(() => {
return light(1000, yellow);
}).then(() => {
return step();
})
}
// step();
const time = (timer) => {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, timer)
})
}
const ajax1 = () => time(2000).then(() => {
console.log(1);
return 1;
})
const ajax2 = () => time(1000).then(() => {
console.log(2);
return 2;
})
const ajax3 = () => time(1000).then(() => {
console.log(3);
return 3;
})
function mergePromise(ajaxArray) {
const data = [];
let promise = Promise.resolve();
ajaxArray.forEach(ajax => {
promise = promise.then(ajax).then(res => {
data.push(res);
return data;
})
})
return promise;
}
mergePromise([ajax1, ajax2, ajax3]).then(data => {
console.log('done');
console.log(data);
})
console