最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

无法在.map()中获取嵌套的异步函数,使其无法在.then()块之前运行

IT培训 admin 13浏览 0评论

无法在.map()中获取嵌套的异步函数,使其无法在.then()块之前运行

我正在构建一个脚本,以生成有关订阅的报告。以下说明该过程:

First Request: get all subscriptions (includes truncated subscriber list)
Second Request: loop through response, fetch complete recipient list for each subscription

Result: Compile report by collated all of the data into a single array

这里的问题是,由于我在代码中使用了await, Promise & async(由工具要求),因此CSV开始在每个订阅的接收者的之后批量GET中创建之前 GET。

结果,我得到的列表实际上只有600(订阅总数),实际上应该超过6,000(所有订阅者加上所有接收者)

我该如何重构它,以使函数util.buildCSV()在所有数据返回并推入report数组后才开始运行?

需要特别注意的是,所有请求函数都是异步的,它们本身只能在异步函数中运行,这很烦人且很困难。

CODE:

const fields = [
  'sub_name',
  'sub_uuid',
  'form_name',
  'form_id',
  'plan_name',
  'plan_id',
  'owner_targetName',
  'owner_firstName',
  'owner_lastName',
  'owner_uuid',
  'applications',
  'lob'
];

(async (env) => {
  const report        = []
  const subscriptions = await xm.subscriptions.getMany(env);

  await Promise.all(subscriptions.map(sub => {
    let apps_and_lobs = xm.subscriptions.process_apps_and_lobs(sub.criteria.data);
    let sub_obj = {
      sub_name         : sub.name,
      sub_uuid         : sub.id,
      form_name        : sub.form.name,
      form_id          : sub.form.id,
      plan_name        : sub.form.plan.name,
      plan_id          : sub.form.plan.id,
      owner_targetName : sub.owner.targetName,
      owner_firstName  : sub.owner.firstName,
      owner_lastName   : sub.owner.lastName,
      owner_uuid       : sub.owner.id,
      applications     : apps_and_lobs.apps,
      lob              : apps_and_lobs.lobs
    };

    report.push(sub_obj);

    if (sub.recipients && sub.recipients.count > 1) {
      (async (env) => {
        let recipients = await xm.subscriptions.getSubscribers(env, null, sub.id);
        recipients.map(r => {
          let recip_obj = {
            sub_name         : sub.name,
            sub_uuid         : sub.id,
            form_name        : sub.form.name,
            form_id          : sub.form.id,
            plan_name        : sub.form.plan.name,
            plan_id          : sub.form.plan.id,
            owner_targetName : r.targetName,
            owner_firstName  : r.firstName,
            owner_lastName   : r.lastName,
            owner_uuid       : r.id,
            applications     : apps_and_lobs.apps,
            lob              : apps_and_lobs.lobs
          };

          report.push(recip_obj);
          console.log(`> ${report.length}`);  // runs after the below COMPLETE > console log showing its breaking in async
        })
      })(env)
    } else {
      console.log(`> ${report.length}`);
    }
  })).then((thing) => {
    util.cmt(`COMPLETE > ${report.length}`);
    util.buildCSV(fields, report, 'subscriptions_dev', true);
  })
})(prod);
回答如下:

尝试一下:

const fields = [
  'sub_name',
  'sub_uuid',
  'form_name',
  'form_id',
  'plan_name',
  'plan_id',
  'owner_targetName',
  'owner_firstName',
  'owner_lastName',
  'owner_uuid',
  'applications',
  'lob'
];

(async (env) => {
  const report        = []
  const subscriptions = await xm.subscriptions.getMany(env);

  await Promise.all(subscriptions.map(async sub => {
    let apps_and_lobs = xm.subscriptions.process_apps_and_lobs(sub.criteria.data);
    let sub_obj = {
      sub_name         : sub.name,
      sub_uuid         : sub.id,
      form_name        : sub.form.name,
      form_id          : sub.form.id,
      plan_name        : sub.form.plan.name,
      plan_id          : sub.form.plan.id,
      owner_targetName : sub.owner.targetName,
      owner_firstName  : sub.owner.firstName,
      owner_lastName   : sub.owner.lastName,
      owner_uuid       : sub.owner.id,
      applications     : apps_and_lobs.apps,
      lob              : apps_and_lobs.lobs
    };

    report.push(sub_obj);

    if (sub.recipients && sub.recipients.count > 1) {
      await (async (env) => {
        let recipients = await xm.subscriptions.getSubscribers(env, null, sub.id);
        recipients.map(r => {
          let recip_obj = {
            sub_name         : sub.name,
            sub_uuid         : sub.id,
            form_name        : sub.form.name,
            form_id          : sub.form.id,
            plan_name        : sub.form.plan.name,
            plan_id          : sub.form.plan.id,
            owner_targetName : r.targetName,
            owner_firstName  : r.firstName,
            owner_lastName   : r.lastName,
            owner_uuid       : r.id,
            applications     : apps_and_lobs.apps,
            lob              : apps_and_lobs.lobs
          };

          report.push(recip_obj);
          console.log(`> ${report.length}`);  // runs after the below COMPLETE > console log showing its breaking in async
        })
      })(env)
    } else {
      console.log(`> ${report.length}`);
    }
  })).then((thing) => {
    util.cmt(`COMPLETE > ${report.length}`);
    util.buildCSV(fields, report, 'subscriptions_dev', true);
  })
})(prod);

更改了两行:

await Promise.all(subscriptions.map(async sub => {


await (async (env) => {

无法在.map()中获取嵌套的异步函数,使其无法在.then()块之前运行

我正在构建一个脚本,以生成有关订阅的报告。以下说明该过程:

First Request: get all subscriptions (includes truncated subscriber list)
Second Request: loop through response, fetch complete recipient list for each subscription

Result: Compile report by collated all of the data into a single array

这里的问题是,由于我在代码中使用了await, Promise & async(由工具要求),因此CSV开始在每个订阅的接收者的之后批量GET中创建之前 GET。

结果,我得到的列表实际上只有600(订阅总数),实际上应该超过6,000(所有订阅者加上所有接收者)

我该如何重构它,以使函数util.buildCSV()在所有数据返回并推入report数组后才开始运行?

需要特别注意的是,所有请求函数都是异步的,它们本身只能在异步函数中运行,这很烦人且很困难。

CODE:

const fields = [
  'sub_name',
  'sub_uuid',
  'form_name',
  'form_id',
  'plan_name',
  'plan_id',
  'owner_targetName',
  'owner_firstName',
  'owner_lastName',
  'owner_uuid',
  'applications',
  'lob'
];

(async (env) => {
  const report        = []
  const subscriptions = await xm.subscriptions.getMany(env);

  await Promise.all(subscriptions.map(sub => {
    let apps_and_lobs = xm.subscriptions.process_apps_and_lobs(sub.criteria.data);
    let sub_obj = {
      sub_name         : sub.name,
      sub_uuid         : sub.id,
      form_name        : sub.form.name,
      form_id          : sub.form.id,
      plan_name        : sub.form.plan.name,
      plan_id          : sub.form.plan.id,
      owner_targetName : sub.owner.targetName,
      owner_firstName  : sub.owner.firstName,
      owner_lastName   : sub.owner.lastName,
      owner_uuid       : sub.owner.id,
      applications     : apps_and_lobs.apps,
      lob              : apps_and_lobs.lobs
    };

    report.push(sub_obj);

    if (sub.recipients && sub.recipients.count > 1) {
      (async (env) => {
        let recipients = await xm.subscriptions.getSubscribers(env, null, sub.id);
        recipients.map(r => {
          let recip_obj = {
            sub_name         : sub.name,
            sub_uuid         : sub.id,
            form_name        : sub.form.name,
            form_id          : sub.form.id,
            plan_name        : sub.form.plan.name,
            plan_id          : sub.form.plan.id,
            owner_targetName : r.targetName,
            owner_firstName  : r.firstName,
            owner_lastName   : r.lastName,
            owner_uuid       : r.id,
            applications     : apps_and_lobs.apps,
            lob              : apps_and_lobs.lobs
          };

          report.push(recip_obj);
          console.log(`> ${report.length}`);  // runs after the below COMPLETE > console log showing its breaking in async
        })
      })(env)
    } else {
      console.log(`> ${report.length}`);
    }
  })).then((thing) => {
    util.cmt(`COMPLETE > ${report.length}`);
    util.buildCSV(fields, report, 'subscriptions_dev', true);
  })
})(prod);
回答如下:

尝试一下:

const fields = [
  'sub_name',
  'sub_uuid',
  'form_name',
  'form_id',
  'plan_name',
  'plan_id',
  'owner_targetName',
  'owner_firstName',
  'owner_lastName',
  'owner_uuid',
  'applications',
  'lob'
];

(async (env) => {
  const report        = []
  const subscriptions = await xm.subscriptions.getMany(env);

  await Promise.all(subscriptions.map(async sub => {
    let apps_and_lobs = xm.subscriptions.process_apps_and_lobs(sub.criteria.data);
    let sub_obj = {
      sub_name         : sub.name,
      sub_uuid         : sub.id,
      form_name        : sub.form.name,
      form_id          : sub.form.id,
      plan_name        : sub.form.plan.name,
      plan_id          : sub.form.plan.id,
      owner_targetName : sub.owner.targetName,
      owner_firstName  : sub.owner.firstName,
      owner_lastName   : sub.owner.lastName,
      owner_uuid       : sub.owner.id,
      applications     : apps_and_lobs.apps,
      lob              : apps_and_lobs.lobs
    };

    report.push(sub_obj);

    if (sub.recipients && sub.recipients.count > 1) {
      await (async (env) => {
        let recipients = await xm.subscriptions.getSubscribers(env, null, sub.id);
        recipients.map(r => {
          let recip_obj = {
            sub_name         : sub.name,
            sub_uuid         : sub.id,
            form_name        : sub.form.name,
            form_id          : sub.form.id,
            plan_name        : sub.form.plan.name,
            plan_id          : sub.form.plan.id,
            owner_targetName : r.targetName,
            owner_firstName  : r.firstName,
            owner_lastName   : r.lastName,
            owner_uuid       : r.id,
            applications     : apps_and_lobs.apps,
            lob              : apps_and_lobs.lobs
          };

          report.push(recip_obj);
          console.log(`> ${report.length}`);  // runs after the below COMPLETE > console log showing its breaking in async
        })
      })(env)
    } else {
      console.log(`> ${report.length}`);
    }
  })).then((thing) => {
    util.cmt(`COMPLETE > ${report.length}`);
    util.buildCSV(fields, report, 'subscriptions_dev', true);
  })
})(prod);

更改了两行:

await Promise.all(subscriptions.map(async sub => {


await (async (env) => {
发布评论

评论列表 (0)

  1. 暂无评论