使用 await 时请求承诺抛出意外的标识符错误
request-promise throwing Unexpected identifier error when using await
以这个简单的GitHubAPI请求为例:
var request = require('request-promise');
var headers = {
'User-Agent': 'YOUR_GITHUB_USERID_HERE'
}
var repos = [
'brandonscript/usergrid-nodejs',
'facebook/react',
'moment/moment',
'nodejs/node',
'lodash/lodash'
]
function requestPromise(options) {
return new Promise((resolve, reject) => {
let json = await request.get(options)
return `${json.full_name} ${json.stargazers_count}`
})
}
(async function() {
for (let repo of repos) {
let options = {
url: 'https://api.github.com/repos/' + repo,
headers: headers,
qs: {}, // or put client_id / client_secret here
json: true
};
let info = await requestPromise(options)
console.log(info)
}
})()
特别是 requestPromise()
下的行,其中我使用了 await
。当 运行 在 Node.js 7.5.0 中,我得到:
$ node --harmony awaitTest.js
awaitTest.js:51
let json = await request.get(options)
^^^^^^^
SyntaxError: Unexpected identifier
at Object.exports.runInThisContext (vm.js:78:16)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:420:7)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:535:3
如果我这样做,而不调用单独的承诺,它会起作用:
(async function() {
for (let repo of repos) {
let options = {}
let json = await request.get(options)
let info = json.full_name + ' ' + json.stargazers_count;
console.log(info) // yay!
}
})()
我可以用 ES5 的方式做到这一点:
request.get(options).then(() => resolve(...info...))
但是当我调用一个单独的 promise 函数时,它不起作用。我怎样才能让它发挥作用?
您似乎正在使用不需要的 Promise 构造函数
只需将requestPromise设置为async,然后就可以进行如下操作
async function requestPromise(options) {
let json = await request.get(options)
return `${json.full_name} ${json.stargazers_count}`
}
以这个简单的GitHubAPI请求为例:
var request = require('request-promise');
var headers = {
'User-Agent': 'YOUR_GITHUB_USERID_HERE'
}
var repos = [
'brandonscript/usergrid-nodejs',
'facebook/react',
'moment/moment',
'nodejs/node',
'lodash/lodash'
]
function requestPromise(options) {
return new Promise((resolve, reject) => {
let json = await request.get(options)
return `${json.full_name} ${json.stargazers_count}`
})
}
(async function() {
for (let repo of repos) {
let options = {
url: 'https://api.github.com/repos/' + repo,
headers: headers,
qs: {}, // or put client_id / client_secret here
json: true
};
let info = await requestPromise(options)
console.log(info)
}
})()
特别是 requestPromise()
下的行,其中我使用了 await
。当 运行 在 Node.js 7.5.0 中,我得到:
$ node --harmony awaitTest.js
awaitTest.js:51
let json = await request.get(options)
^^^^^^^
SyntaxError: Unexpected identifier
at Object.exports.runInThisContext (vm.js:78:16)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:420:7)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:535:3
如果我这样做,而不调用单独的承诺,它会起作用:
(async function() {
for (let repo of repos) {
let options = {}
let json = await request.get(options)
let info = json.full_name + ' ' + json.stargazers_count;
console.log(info) // yay!
}
})()
我可以用 ES5 的方式做到这一点:
request.get(options).then(() => resolve(...info...))
但是当我调用一个单独的 promise 函数时,它不起作用。我怎样才能让它发挥作用?
您似乎正在使用不需要的 Promise 构造函数
只需将requestPromise设置为async,然后就可以进行如下操作
async function requestPromise(options) {
let json = await request.get(options)
return `${json.full_name} ${json.stargazers_count}`
}