我已经使用 Promise 在 Node.js 中编写了大约六个函数,我真的想发布所有这些代码,而不是发布一个模拟示例,这样我就可以简洁地封装我的问题。所以说我有以下两个功能:
foo = () => {
return new Promise( ( r , rj ) => {
setTimeout( () => {
r('DONE');
}, 3000 );
});
}
And
bar = () => {
return new Promise( (r , rj) => { r('ALL DONE !') } )
}
现在我想避免回调地狱并执行以下操作:
foo().then( (resp) => console.log(resp) ).bar()
相反,我被迫做的是:
foo().then( (resp) => { console.log(resp); bar() } )
所以到目前为止,基本上在我的生产代码中我有类似下面的内容(只是为了给你一个想法):
let uploadToVault = ( INPUT_DIR , VOLT_CRED ) => {
INPUT_DIRECTORY = INPUT_DIR;
VOLT_CREDENTIALS = VOLT_CRED;
volt_APILogin().then( () => {
volt_getProduct().then( () => {
volt_CreatePresentation().then( (resp) => {
console.log(resp);
volt_uploadSlides().then( (resp) => {
console.log(resp);
volt_bindSlide().then( (resp) => {
console.log(resp);
});
});
});
});
});
}
现在我怎样才能以更多的链格式编写它而不是在回调中编写它?