【问题标题】:How to add async/await to my functions in nodejs?如何在 nodejs 中向我的函数添加异步/等待?
【发布时间】:2021-04-15 00:39:08
【问题描述】:

我试图使代码异步,但我做不到。我需要做什么? 这是我的功能:

1.

router.post('/urls', (req, response) => {
  count = 2;
  webUrl = req.body.url;
  depth = req.body.depth;
  letstart(webUrl, response);
});
function letstart(urlLink, response) {
  request(urlLink, function (error, res, body) {
    console.error('error:', error); // Print the error if one occurred
    console.log('statusCode:', res && res.statusCode); // Print the response status code if a response was received
    //console.log('body:', body); // Print the HTML for the Google homepage.
    if (!error) {
     getLinks(body);
      if (!ifFinishAll) {
       GetinsideLinks(linkslinst, response);
      }
      else {
        console.log("Finish crawl");
      }
    }
    else {
      console.log("sorry");
      return "sorry";
    }
  });
}
function GetinsideLinks(list, response) {
  count++;
  if (count <= depth) {
    for (let i = 0; i < list.length; i++) {
      const link = list[i].toString();
      var includeUrl = link.includes(webUrl);
      if (!includeUrl) {
        request(link, function (error, res, body) {
          console.error('error2:', error); // Print the error if one occurred
          console.log('statusCode2:', res && res.statusCode); // Print the response status code if a response was received
          if (!error) {
            getLinks(body);
          }
          else {
            console.log("sorry2");
          }
        });
      }
    }
    ifFinishAll = true;
  }
  else {
    console.log("finish");
    ifFinishAll = true;
    response.status(200).send(resArray);
  };
  return resArray;
}
function getLinks(body) {
  const html = body;
  const $ = cheerio.load(html);
  const linkObjects = $('a');
  const links = [];
  linkObjects.each((index, element) => {
    countLinks = linkObjects.length;
    var strHref = $(element).attr('href');
    var strText = $(element).text();
    var existUrl = linkslinst.includes(strHref);
    var existText = textslist.includes(strText);
    if (strText !== '' && strText !== "" && strText !== null && strHref !== '' && strHref !== "" && strHref !== null && strHref !== undefined && !existUrl && !existText) {
      var tel = strHref.startsWith("tel");
      var mail = strHref.startsWith("mailto");
      var linkInStart = isUrlValid(strHref);
      if (!tel && !mail) {
        if (linkInStart) {
          links.push({
            text: $(element).text(), // get the text
            href: $(element).attr('href'), // get the href attribute
          });
          linkslinst.push($(element).attr('href'));
          textslist.push($(element).text());
        }
        else {
          links.push({
            text: $(element).text(), // get the text
            href: webUrl.toString() + $(element).attr('href'), // get the href attribute
          });
          linkslinst.push(webUrl.toString() + $(element).attr('href'))
          textslist.push($(element).text());
        }
      }
    }
  });
  const result = [];
  const map = new Map();
  for (const item of links) {
    if (!map.has(item.text)) {
      map.set(item.text, true);    // set any value to Map
      result.push({
        text: item.text,
        href: item.href
      });
    }
  }
  if (result.length > 0) {
    resArray.push({ list: result, depth: count - 1 });
  }
  console.log('res', resArray);
  return resArray;
}

我想最终返回/响应“resArray”。我尝试将 async 和 await 添加到函数 1 和 2,但它没有成功。也许我需要将 async/await 添加到所有功能?我该如何解决?

【问题讨论】:

标签: javascript node.js async-await


【解决方案1】:

您可以使用async-await 来实现您的目标。

异步函数是使用 async 关键字声明的函数,其中允许使用 await 关键字。 async 和 await 关键字使异步的、基于 Promise 的行为能够以更简洁的方式编写,避免了显式配置 Promise 链的需要。

基本示例:

function resolveImmediately() {
  return new Promise(resolve => {
    resolve(true);
  });
}

function resolveAfter2Seconds() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved');
    }, 2000);
  });
}

async function asyncCall() {
  console.log('calling');
  const result = await resolveImmediately();
  console.log(result);
  if(result) {
     const anotherResult = await resolveAfter2Seconds();
     console.log(anotherResult);
  }
}

asyncCall();

注意:您的代码太长,无法调试。因此,为了让您了解该方法(做什么和如何做),我在答案中添加了一个简单的示例。

【讨论】:

    猜你喜欢
    • 2020-09-23
    • 2020-01-09
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 2020-03-31
    • 2018-01-15
    相关资源
    最近更新 更多