3 ms·
It also allows for workflows that are really hard with promise chains alone, even if what it does isn't so simple.. while (somecondition) { try {
by tracker1 11y ago
It also allows for workflows that are really hard with promise chains alone, even if what it does isn't so simple..
while (somecondition) {
try {
await sleep(1000);
somecondition = await someAsyncAction();
} catch(err) {
}
}
Doing retries, or loops against promise actions are really cumbersome with promises directly, but with async/await becomes much more clear in terms of action and intent.
- jlongster 11y agoYou can do this today with generators.
- tracker1 11y agoTo use generators, I need something like Babel, so I may as well use async/await.
- bluepnume 11y agoThat said, it isn't too hard to write re-usable helpers for promise based control flow, in a similar manner to what async.js does for callbacks. function iff(condition, method) { return Promise.try(condition).then(result => { if (result) { return method(); } }); } function whilst(condition, method) { return iff(condition, () => { return Promise.try(method) .then(whilst.bind(null, condition, method)); }); } whilst(someAsyncAction, Promise.sleep.bind(null, 1000)) .catch(err => { // ... }) Naturally you wouldn't want to re-implement all of this control flow every time you need it. But the end result is not too much boilerplate.