4

I'm not really understanding the difference between this code:

co(function *() {
    const val = yield aPromise();
    return val;
})
.then((val) => doSomethingWith(val), (err) => doSomethingWith(err));

and this other one:

async function () {
    try {
        const val = await aPromise();
        doSomethingWith(val);
    } catch (err) {
        doSomethingWith(err);
    }
}

What are the pros/cons (in terms of performance, readability and flow control mainly) of each code used in a browser or in server (node.js) and why should co (which depends on co external library) or await (which is not part of ES7 yet and depends on babel-polyfill) be used.

Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
David
  • 2,511
  • 14
  • 23
  • I'm voting to close this question as off-topic because the comparison doesn't make sense. – Amit Oct 04 '16 at 08:24
  • Overkill in both cases :). `aPromise().then()` would be enough. :) – Yury Tarabanko Oct 04 '16 at 08:24
  • @Amit why it does not make sense? Could you explain a little bit to better understand? The main problem is that I can't see why it does not make sense... – David Oct 04 '16 at 08:26
  • 1
    There are many ways to do virtually everything in the Javascript world. Comparing each one against each other is hardly productive. There's a clear difference in syntax, which is the obvious difference (apart from the external dependencies). Take your pick. – deceze Oct 04 '16 at 08:26
  • @deceze so there is no difference in performance? – David Oct 04 '16 at 08:27
  • 1
    If you're concerned about performance, **benchmark** it. – deceze Oct 04 '16 at 08:28
  • It doesn't make sense since the `await` version isn't implemented anywhere, and can only be transpiled to roughly the first version. That's like asking when would you use source code vs. compiled code. – Amit Oct 04 '16 at 08:36
  • *"which is not part of ES7 yet*" and it never will be. ES7 (ES2016) was released this year. async/await will be released next year as part of ES2017. – Felix Kling Oct 21 '16 at 06:06

1 Answers1

5

Two major differences for the example code you've shown:

Now, I think what you actually wanted to compare are

var example = co.wrap(function *() {
    try {
        const val = yield aPromise();
        doSomethingWith(val);
    } catch (err) {
        doSomethingWith(err);
    }
})

and

async function example() {
    try {
        const val = await aPromise();
        doSomethingWith(val);
    } catch (err) {
        doSomethingWith(err);
    }
}

and possibly

function example() {
    return aPromise().then(doSomethingWith).catch(doSomethingWith);
}

(the last one actually has a different behaviour if aPromise throws synchronously, which it of course should never do)

So lets discuss

performance

Does not matter. Really not. Although the third one might be the fastest because it creates the least amount of promises, and the other two are not yet optimised well in engines.

readability

Choose yourself. The first two are pretty much equivalent, though co is a bit ugly (abusing generator syntax). The third is pretty concise and could be favoured because of that, although this advantage is quickly lost for everything with complex control flow.

flow control

That's not a question of pros/cons, it has to be the one you want.

why should co or await be used?

co should not be used any more, it's superseded by the standard ES8 (ES2017) async/await (which is not yet published, but still). It might still be used as a transpiler target (for environments that support ES6 but not ES8) or for backwards-compatibility when it was used with something else than promises (given that co supports more types of "yieldables").

aleclarson
  • 15,865
  • 9
  • 52
  • 80
Bergi
  • 513,640
  • 108
  • 821
  • 1,164