[Nodejs] BBB, Babel Burger Boilerplate
Nowadays coding is like eating fast food: make fast, eat fast, as long as the job was done before deadline. You will make many mistakes, You will come back for more.
During the development cycle, half of time is spent on new frameworks and breaking down features, another half of the time is implementing, the goal of the story is shorten time of environment configuration, and hit the ground to coding.
Recently, I am playing around ES7 async/await in Node.js, which needs an ES7 environment. There are couple of methods base on the great SDK babeljs.io to accomplish the environment, and I pick up one as the boilerplate and for share.
Remark: This story is for Node v4. and the lastest Node supports native ES7, thanks Stefan Bobev
Step 1 — clone the template from github, and install.
$ git clone git@github.com:wahengchang/node-es7-babel-template.git
$ npm install
Step 2 — Basically the environment was setup, let’s try something in ES7 to see if it works well. Go and Edit “main.js” file, which is a default entry of ES7 compiler, there is a sleep() function which is a Promise function, and it will be called by the amazing ES7 after.
// ./.gitignore
// ./README.md
// ./es7compiler.js
// ./main.js
// ./package.json//main.js
module.exports = function() { (async function() {
var sleep = function(para) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log('para: ', para)
resolve(para * para)
}, 1000)
})
} var result = await sleep(2);
console.log('result: ', result);
}());}
Step 3 — Run the script which edited on Step 2, done.
$ npm start
//para: 2
//result: 4
Reference:
github:
https://github.com/wahengchang/node-es7-babel-template
babel must read:
http://babeljs.io/learn-es2015/
Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!