function compose(...funcs) {
if (funcs.length === 0) {
return args => args;
}
if (funcs.length === 1) {
return funcs[0];
}
return funcs.reduce((a,b)=>{
return (...args) => a(b(...args));
});
}
const abcd = compose((c)=>c*10,(a,b)=>(a+b));
console.log(abcd(1,2));
function composePromise(...promises) {
console.log(promises);
const init = promises.pop();
return function(...args) {
return promises.reverse().reduce((sequence, func)=>{
return sequence.then(function(result){
return func.call(null,result)
})
}, Promise.resolve(init.apply(null, args)));
}
}
let pa = () => {
return new Promise((resolve)=>{
setTimeout(()=>{
console.log('pa');
resolve(1111);
},1000)
})
}
let pb = () => {
return new Promise((resolve)=>{
setTimeout(()=>{
console.log('pb');
resolve(2222);
},2000)
})
}
const abc = composePromise(pa,pb);
abc().then(res => { console.log(666) })
abc(12312313);
console