vadimdemedes / ama

Ask Me Anything!
2 stars 0 forks source link

What's your favorite new ES2015 feature and why? #3

Closed arthurvr closed 9 years ago

vadimdemedes commented 9 years ago

Generator functions and only generator functions.

For me, it is the only new feature that really improves the way I write code every day. Yes, arrow functions are nice, yes, classes are nice, but we could do all the same things before, right? Generator functions allow us to do things, that were not possible before!

Generators alone don't give much benefit, but if you use them with co and koa - Oh. My. God.

I've never had so much pleasure writing JavaScript before. It's just beautiful:

let post = yield Post.findOne();
post.title = 'New title';
yield post.save();

I know it's unofficial way of using generators, but with ES7 async/await, all we have to do is to switch function * myFn () to async myFn () and yield myFn() to await myFn() and we are golden.

arthurvr commented 9 years ago

Thanks for the answer! :)