1

我有以下片段

"use strict"

const req = require('requisition');

async function doRequest () {
  const url = 'http://api.com/v3/search?q=breno'
  const res = await req.get(url)

  console.log(res.status)

  const body = await res.json();

  return "it Works!"
}


console.log(doRequest())

请求工作得很好,但 console.log() 产生:

{}
200

代替

200
"it Works!"

当我尝试:

console.log(await doRequest())

我收到一个Unexpected Token错误

4

1 回答 1

2

async函数返回承诺。在顶层,您必须“订阅”承诺:

doRequest().then(result => console.log(result));
于 2015-11-11T20:19:08.497 回答