Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


That 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.


You can do this today with generators.


To use generators, I need something like Babel, so I may as well use async/await.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: