ECMASCRIPT ES8 and Beyond
From ES6 to ES-next
I’m writing this post with a lot of excitement. I’ve decided to play around with new ECMAScript proposals since ES6 has already been out there for quite a long time and ES7 comes with just two new features (which are the exponentiation operator (**) and the Array.prototype.includes() function).
Unveiling ES-next Features with Presets
Thanks to Babel’s stage-0 preset we can already use things that will be included in ES8 or later specifications, or even those that are at just proposals. Babel offers us quite a few presets — depending of how far we want to go in ES-next — and we can choose the preset that we feel like trying out. Here’s the list of currently available presets:
- stage-0 — Strawman: just an idea, possible Babel plugin.
- stage-1 — Proposal: this is worth working on.
- stage-2 — Draft: initial spec.
- stage-3 — Candidate: complete spec and initial browser implementations.
- stage-4 — Finished: will be added to the next yearly release.
I’ll present some features from each preset — to hopefully get you to use this amazing transpiler and boost your code with some syntax improvements.
Diving into stage-3: The async-await marvel
I’ll start with my favorite feature of the stage-3 preset which is the transform-async-to-generator plugin. It adds two new keywords to our code, namely async and await. I’m using them constantly in my backend applications.
Here is the basic syntax:
It works just like generators — the await keyword makes our application wait until our promise is resolved. It’s very useful for API or DB calls. Here’s a small real-life example:
And that’s all — the result of our promise is assigned to ‘user’. The power of this transform is that it doesn’t use regenerator or any other runtime but desugars to native generators that are already supported on Node 6.x and other environments. You can get rid of a pyramid of callbacks in .then() functions, and make your code clean and readable.
Revolutionizing objects with stage-2: The object rest-spread plugin
Our next preset is stage-2 — it comes with one plugin only, but trust me — it’s a good one. Plugin transform-object-rest-spread is a game-changer. Using it, you can rest and spread object properties and reduce the amount of code you have to write in this specific use case. Here’s an example:
Amazing, isn’t it? If you haven’t been using it yet — now it’s about time you start. The deeper we go into the new ECMAScript proposals, the more amazing features we can use.
Stage-1 wonders: A glimpse at transform-class-properties
Proposal preset stage-1 used to give us the transform-decorators plugin — but since there is a pending proposal update — it has been temporarily disabled. But worry not — we will look at another amazing feature, the transform-class-properties plugin. It’s very useful with react (DOM or native) — technologies. Now you can use the following syntax to bind a property to a class:
You can use it to define state or check propTypes in react classes as follows:
Stage-0 marvels: Simplifying code with the bind operator
Last but not least, the stage-0 preset comes with my favourite ES-nextproposal — the bind operator (::). If you are tired of using .call() .bind()and .apply() functions, this syntax is just for you. Babel gives us an example below:
But i think you’ll see it all better in this example. Here is the code written in ES5:
And how we can make it simpler using the new syntax. Take a look:
This iterator comes with a variation of the for-of iteration statement, so you can iterate through your promises with syntax like this:
Peeking into the future