📜  node js请求异步等待 - Javascript代码示例

📅  最后修改于: 2022-03-11 15:03:21.748000             🧑  作者: Mango

代码示例1
function doRequest(url) {
  return new Promise(function (resolve, reject) {
    request(url, function (error, res, body) {
      if (!error && res.statusCode == 200) {
        resolve(body);
      } else {
        reject(error);
      }
    });
  });
}

// Usage:

async function main() {
  let res = await doRequest(url);
  console.log(res);
}

main();