ES7's async/await is the ultimate way. Use with Babel and you can run it anywhere.
async function main() {
await signIn()
const res = await http.get('/articles')
const articles = res.data
try {
const updatedArticle = (await http.put(`/article/${articles[0].id}`, { data: { title: 'something' } })).data
articles[0] = updatedArticle
}
catch (err) {
console.log('error', err)
}
for (let article of articles) {
await something(article)
}
const arrayOfAsyncOperations = [ update(x), update(y) ]
await *arrayOfAsyncOperations
if (something) {
await anotherOperation()
}
await signOut()
}
Writing this code with promises is a nightmare, especially when it comes to conditions/loops. async/await keep it super clean.
Generators are less fancy.