[Nodejs] BBB, Babel Burger Boilerplate

Written by peterchang_82818 | Published 2016/12/08
Tech Story Tags: nodejs | template | es7 | tutorial | babel

TLDRvia the TL;DR App

pixabay.com

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.jsmodule.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/


Published by HackerNoon on 2016/12/08