Some of new ECMAScript features can boost your productivity a lot.
I would like to introduce some of my favorite features!
๐ฃ Fetch JSON with async/await
Fetch JSON by URL is as follows:
async function fetchAsync() { |
The above code runs in the above environment:
- Async/Await : Latest Chrome/FF browser or Babel: https://babeljs.io/docs/plugins/transform-async-to-generator/
- Fetch : Latest Chrome/FF browser or Github fetch polyfill: https://github.com/github/fetch
๐ Spread syntax
Spread syntax allows an iterable such as an array expression or string to be expanded.
Some examples for array are as follows:
// Arguments to function |
In addition, some examples of object literals:
const obj1 = { foo: 'bar', x: 42 }; |
๐ DataStruct Assignment
The DataStruct Assignment syntax makes it possible to unpack values from arrays or properties from objects.
// Object literals |
๐ Async / Await
- When an
async
function is called, it returns aPromise
- When the
async
function returns a value, thePromise
will be resoleved with the returned value - WHen the
async
function throws an exception or some value, thePromise
will be rejected with the throw value
An async
function can contain an await
expression, that pauses the execution of the async function and waits for the passed Promise
โs resolution, and then resumes the async functionโs exection and returns the resolved value.
A simple example for await/async
are as follows:
function resolveAfter2Seconds(x) { |
When you use async/await
, You can handle error by try-catch
, like this:
function shouldBeFail(){ |
๐ผ Special Thanks
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
- https://qiita.com/berlysia/items/ce14f023f10100e35d35
- https://gist.github.com/msmfsd/fca50ab095b795eb39739e8c4357a808
๐ฅ Recommended VPS Service
VULTR provides high performance cloud compute environment for you.
Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour).
In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!