安裝:
> npm install --save-dev babel-cli babel-preset-es2017
> echo '{ "presets": ["es2017"] }' > .babelrc
測試程式(a.js)如下:
1: async function Insert(data) {
2: return new Promise(resolve => {
3: console.log('insert data '+data);
4: resolve();
5: });
6: }
7: async function Hello(data_from_client) {
8: await Insert(data_from_client);
9: await Insert(data_from_client+1);
10: await Insert(data_from_client+2);
11: console.log('transaction finished');
12: };
13: Hello(1);
直接執行:
> babel-node a.js
從ES2017轉舊版js(b.js)
> babel a.js --presets es2017 > b.js
Ubuntu安裝:
需要多安裝一個env套件才有es2015支援
> npm install babel-preset-env --save-dev
> echo '{ "presets": ["env", "es2017"] }' > .babelrc
參考來源:
https://babeljs.io/docs/plugins/preset-es2017/#usage-via-babelrc-recommended