Generator Arrow Functions

Axel Rauschmayer axel at rauschma.de
Fri Nov 15 08:34:01 PST 2013


> It would be great to have await, but in the meantime having generator functions would help male async methods tolerable. Await is ES7 at the earliest, generator arrow functions could be in ES6.


Couldn’t arrow generator functions replace generator function declarations? In other words: is the dynamic `this` in generator function declarations ever useful?

Then we’d have a nice symmetry in ES6:

– non-method function = const + arrow function.
– method = concise method definition

– non-method generator function = const + arrow generator function.
– generator method = concise generator method definition


That would make the async programming code more compact, too (I’m assuming a nullary paren-free arrow variant and I prefer the asterisk after the arrow):

```js
spawn(=>* {
    var data = yield $.ajax(url);
    $('#result').html(data);
    var status = $('#status').html('Download complete.');
    yield status.fadeIn().promise();
    yield sleep(2000);
    status.fadeOut();
});
```

Versus:

```js
spawn(function*() {
    var data = yield $.ajax(url);
    $('#result').html(data);
    var status = $('#status').html('Download complete.');
    yield status.fadeIn().promise();
    yield sleep(2000);
    status.fadeOut();
});
```

[Example taken from task.js website.]

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131115/8779198a/attachment.html>


More information about the es-discuss mailing list