链接在瀑布承诺

我一直在玩一些链接一系列function的不同方法,而且似乎找不到一个我特别喜欢的方法。 以下是我最后一个解决的问题,但我仍然不热衷于此。

有人可以build议一个更干净,更简洁的模式? 我不想selectAsync.js或库。

[ this.connectDatabase.bind(this), this.connectServer.bind(this), this.listen.bind(this) ].reduce( (chain, fn) => { let p = new Promise(fn); chain.then(p); return p; }, Promise.resolve() ); 

PS。 任何其他的提示都比欢迎。

在stackoverflow上find这个解决scheme,你可以dynamic链接承诺:

 iterable.reduce((p, fn) => p.then(fn), Promise.resolve()) 

完整的文章在这里: https : //stackoverflow.com/a/30823708/4052701

什么ES7asynchronous/等待? 奇怪/旧的绑定(这)在你的代码,但不要混淆你的例子。

 async function x() { try { await this.connectDatabase.bind(this); await this.connectServer.bind(this); await this.listen.bind(this); } catch(e) { throw e; } } 

或更通用

 async function () { for (let item of yourArray) { try { await item.bind(this); //strange bind of your code. } catch(e) { throw e; } } }