繁体   English   中英

修复嵌套的承诺链

[英]Fix a nested promise chain

我有一个现有的 Promise 链,这让我很不舒服。 我不喜欢嵌套的回调,更不用说嵌套的承诺链了。 我已经重构了它,但很好奇是否可以做得更好。

重构的原因是我需要在这个流程中添加一个额外的函数调用,就在中间😎

现存的:

initFirestore()
  .then(() => {
    console.log('Firestore has been initialized.');
    seedServices()
      .then(customerId => {
// here is where my new function would need to live.
        seedFirestore()
          .then(() => {
            console.log('Seed created for Customer: ', customerId);
          })
          .catch(err => {
            console.log('Trouble seeding firestore with seedTemplates', err);
          });
      })
      .catch(customerId => {
        console.log('Seed created with warnings for  Customer: ', customerId);
      });
  })
  .catch(err => {
    throw new Error(err);
  });

我建议的更新:

initFirestore()
  .then(() => {
    console.log('Firestore has been initialized.');
  })
  .catch(err => {
    throw new Error(err);
  })
  .then(() => seedServices())
  .then(customerId => customerId)
  .catch(err => {
    console.log('Trouble seeding firestore with seedTemplates', err);
  })
  .then(async customerId => {
    await myNewFunction().then(
      (response) => {
        console.log(response);
      }
    );
    return customerId;
  })
  .then(async customerId => {
    await seedFirestore();
    return customerId;
  })
  .then(customerId => {
    console.log('Seed created for Customer: ', customerId);
  })
  .catch(customerId => {
    console.log('Seed created with warnings for  Customer: ', customerId);
  });

我对链中的catch混合不满意......我还能怎么做?

实际上你在做什么是可以的,如果你想捕捉错误,记录一个警告然后继续。

这是异步/等待等价物:

let customerId;
try {

    try {
      await initFirestore()
      console.log('Firestore has been initialized.');
    } catch(err) {
      throw new Error(err);
    });

    try {
      customerId = await seedServices());
    } catch(err) {
      console.log('Trouble seeding firestore with seedTemplates', err);
    };

    const response = await myNewFunction();
    console.log(response);

    await seedFirestore();
    console.log('Seed created for Customer: ', customerId);

} catch (err) {
   console.log('Seed created with warnings for  Customer: ', customerId);
}

这里奇怪的是:

  1. 立即重新抛出你得到的错误,包裹在一个new Error()
  2. 使用异常进行警告

而且,您可以通过将customerId变量提升到第一个之外来稍微清理一下代码,因此您不必到处传递它。

编辑您的代码有问题,并非每个路径都会正确地将 customerId 转发到下一个 then() 处理程序。 如果你想使用 then()/catch() 而不是 async/await。

我要问的一个问题是,即使在 seedServices 失败后,您是否真的想继续操作。 我的猜测是“不”,所以这会导致我将您的代码重写为:

let customerId;

initFirestore()
  .then(() => {
    console.log('Firestore has been initialized.');
    return seedServices();
  })
  .then(cId => {
    customerId = cId;
    return myNewFunction();
  })
  .then(() => seedFireStore());
  .then(() => {
    console.log('Seed created for Customer: ', customerId);
  })
  .catch(customerId => {
    console.log('Error while creating seed for customer ', customerId);
  });

异步/等待等效:

let customerId;

try {
  await initFirestore()
  const customerId = await seedServices();
  await myNewFunction();
  await seedFireStore());

  console.log('Seed created for Customer: ', customerId);

} catch(err) {
  console.log('Error while creating seed for customer ', customerId);
});

如果您确实想捕获错误并继续(在 seedServices 之后),我的建议是将您的操作拆分为几个不同的功能。 这总是比嵌套你的承诺链更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM