Node.js 呈现两个单独的async.each方法的结果

Node.js 呈现两个单独的async.each方法的结果,node.js,async.js,Node.js,Async.js,我不熟悉nodejs和async。在理解如何将两个单独的async.each方法包装为一个res.render时遇到困难…我正在尝试在前端显示有效帐户ID和有效用户ID的列表。 两个单独的async.com方法分别是: async.each(account_ids, function(accountId, callback) { console.log('Processing accountId ' + accountId); callingExternalAp

我不熟悉nodejs和async。在理解如何将两个单独的async.each方法包装为一个res.render时遇到困难…我正在尝试在前端显示有效帐户ID和有效用户ID的列表。 两个单独的async.com方法分别是:

async.each(account_ids, function(accountId, callback) {
        console.log('Processing accountId ' + accountId);

        callingExternalApi(accountId, callback, function(err, response){
          if(err){
            console.log("error account");
          }
          console.log("account response is: ", response);
        });

      }, function(err) {
        if( err ) {
          console.log('An account failed to process');
        } else {
          console.log('All accounts have been processed successfully');
        }
      });

非常感谢您的帮助。请原谅任何冗余回调或错误记录。仍在学习nodej


谢谢

主要问题不是您正在调用这两个async.each调用。问题是它们将并行运行,调用req.*函数或回调函数的最快函数将向连接返回响应

如果省略它们的回调参数,这两个函数都会返回承诺

一般来说,我建议阅读async库和JS async/await:

请注意,async还接受本机异步函数,这使许多查找程序更干净、更容易理解

以下是我认为您希望从上述代码中获得的信息,包括将结果编译成列表:

var request = require("request-promise");

async function checkAccounts(account_ids) {
  const valid_accounts = [];
  await async.each(account_ids, async function(accountId) {
    console.log("Processing accountId " + accountId);
    const extAPIresult = await callingExternalApi(accountId);
    console.log("account response is: ", extAPIresult);
  });
  valid_accounts.push(extAPIresult);
  console.log("All accounts have been processed successfully");
  return valid_accounts;
}

async function checkEmails(email_ids) {
  const valid_emails = [];
  await async.each(email_ids, async function(emailId) {
    console.log("Processing email id " + emailId);
    const reqresult = await request({
      url: emailIdlookupUrl,
      method: "POST",
      json: {
        email_address: emailId
      }
    });
    if (reqresult.statusCode !== 200) {
      throw new Error("Unable to verify user");
    }
    valid_emails.push(reqresult.body.user.id);
    console.log("user id is: ", reqresult.body.user.id);
  });
  console.log("All emails have been processed successfully");
  return valid_emails;
}

async function doChecks() {
  const accounts = checkAccounts(account_ids);
  const emails = checkEmails(email_ids);
  const responses = await Promises.all([accounts, emails]);
  console.log("All checks have been processed successfully");
  return responses;
}

function get(req, res) {
  doChecks()
    .then(responses => {
      res.send("All checks have been processed successfully");
      res.send(String(responses));
    })
    .catch(err => {
      req.flash("error", err.message);
      res.redirect("?");
    });
}
var request = require("request-promise");

async function checkAccounts(account_ids) {
  const valid_accounts = [];
  await async.each(account_ids, async function(accountId) {
    console.log("Processing accountId " + accountId);
    const extAPIresult = await callingExternalApi(accountId);
    console.log("account response is: ", extAPIresult);
  });
  valid_accounts.push(extAPIresult);
  console.log("All accounts have been processed successfully");
  return valid_accounts;
}

async function checkEmails(email_ids) {
  const valid_emails = [];
  await async.each(email_ids, async function(emailId) {
    console.log("Processing email id " + emailId);
    const reqresult = await request({
      url: emailIdlookupUrl,
      method: "POST",
      json: {
        email_address: emailId
      }
    });
    if (reqresult.statusCode !== 200) {
      throw new Error("Unable to verify user");
    }
    valid_emails.push(reqresult.body.user.id);
    console.log("user id is: ", reqresult.body.user.id);
  });
  console.log("All emails have been processed successfully");
  return valid_emails;
}

async function doChecks() {
  const accounts = checkAccounts(account_ids);
  const emails = checkEmails(email_ids);
  const responses = await Promises.all([accounts, emails]);
  console.log("All checks have been processed successfully");
  return responses;
}

function get(req, res) {
  doChecks()
    .then(responses => {
      res.send("All checks have been processed successfully");
      res.send(String(responses));
    })
    .catch(err => {
      req.flash("error", err.message);
      res.redirect("?");
    });
}