将Node.js的request模块进行Promise封装,并通过async/await进行调用


const request = require(`request`);

function doRequest(options) {
  return new Promise(function (resolve, reject) {
    request(options, function (error, res, body) {
      if (!error && res.statusCode == 200) {
        resolve(body);
      } else {
        reject(error);
      }
    });
  });
}

如果这样的话 dehuà)

const hoge = async () =>{
  const requestOptions = {
      url: url,
      method: "POST",
      headers: headers,
      json: payload
    }
  const res = await doRequest(requestOptions)
}

广告
将在 10 秒后关闭
bannerAds