node v6.9.2下使用async/await的方法


使用babel,配置.babelrc

1
2
3
4
5
6
7
8
9
{
    "presets": [
        ["env", {
            "targets": {
                "node": true
            }
        }]
    ]
}

相关依赖package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
    "name": "xxxx",
    "version": "1.0.0",
    "description": "xxxx",
    "main": "./public/index.js",
    "scripts": {
        "test": "node ./public/index.js"
    },
    "keywords": [
        "xxxx"
    ],
    "author": "xxx",
    "license": "ISC",
    "devDependencies": {
        "babel-cli": "^6.24.1",
        "babel-plugin-transform-async-to-generator": "^6.24.1",
        "babel-plugin-transform-runtime": "^6.15.0",
        "babel-polyfill": "^6.23.0",
        "eslint": "^4.0.0",
        "eslint-config-google": "^0.8.0",
        "eslint-plugin-react": "^7.1.0"
    },
    "dependencies": {
        "babel": "^6.23.0",
        "babel-core": "^6.18.2",
        "babel-preset-env": "^1.6.0",
        "babel-preset-es2015": "^6.18.0",
        "babel-preset-stage-3": "^6.17.0",
        "babel-register": "^6.24.1",
        "babel-runtime": "^6.18.0",
        "bluebird": "^3.4.6",
    }
}

配置index.js

1
2
require('babel-register');
require('./app.js');

app.js为项目主运行文件

node ./public/index.js