A chain of promises in a waterfall

I played with several different ways of chaining a feature set and didn't seem to find what I particularly like. The following is the last one I settled on, but I'm still not addicted to it.

Can someone suggest a cleaner and more concise template? I do not want to choose Async.js or the library.

[ 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. Any other tips are more than welcome.

+5
source share
2 answers

This solution was discovered in stackoverflow on how you can chain promises dynamically:

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

The full message is here: fooobar.com/questions/99628 / ...

+5
source

What about ES7 async / wait? Strange / old bind (this) in your code, but should not be confused with your example.

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

or more general

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

Source: https://habr.com/ru/post/1259697/


All Articles