... after few years...
ES6 has been adopted as standard , and ES7 is around the corner, so it deserves an updated answer. As an example, we will use ES6 + async / await with nodejs + babel, the necessary conditions are:
An example of your foo.js file might look like this:
import { exec } from 'child_process'; async function sh(cmd) { return new Promise(function (resolve, reject) { exec(cmd, (err, stdout, stderr) => { if (err) { reject(err); } else { resolve({ stdout, stderr }); } }); }); } async function main() { let { stdout } = await sh('ls'); for (let line of stdout.split('\n')) { console.log(`ls: ${line}`); } } main();
Make sure you have babel:
npm i babel-cli -g
Install the latest preset:
npm i babel-preset-latest
Run it through:
babel-node --presets latest foo.js
Mirek Rusin Aug 08 '15 at 19:48 2015-08-08 19:48
source share