异步或承诺全部,如果第一次失败,则全部拒绝 - 3个后方法调用

发布于 2025-01-26 09:51:37 字数 379 浏览 4 评论 0原文

编辑。我需要调用adduser()以接收userId从数据库生成的回来。然后,我需要用userId来调用addticket()以接收ticketid从数据库生成的返回。然后用用户IDticketid call 呼叫addtobooks。这些都是POST方法,每个调用中我需要返回的唯一数据是他们的id s,最后一个帖子方法将返回完整的数据。如果该过程中的任何一个失败,我是否可以处理所有帖子方法的方法?我已经看到了Promise的示例。所有可以做到这一点,但这全是为了获得方法。

Edited. I need to call addUser() to receive the userId back thats generated from the database. Then i need to call addTicket() with the userId to receive the ticketId back thats generated from the database. Then finally call addToBooks with the userId and ticketId. These are all post methods, the only data I need back from each call are their ids, the last post method will return full data. Is there a way to handle this where I can fail all post methods if any one of the fails in the process? I've seen examples of promise.all that can do this but it was all for get methods.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒猫 2025-02-02 09:51:37

您可以尝试这样的尝试,按照您的函数名称示例:

const getData = async () => {
  try {
    const userId = await addUser();
    if(userId) {
       const ticketId = await addTicket(userId);
       const response = await addToBooks(userId, ticketId);
       return response;
    } else {
       throw new Error("Error getting data")
    }
  } catch(error) {
     console.log(error);
  }
}

这不使用Promise.all,但是由于您为我理解的示例,只需要userId 要完成其他两个操作,我认为这应该有效

You could try something like this, following your functions names example:

const getData = async () => {
  try {
    const userId = await addUser();
    if(userId) {
       const ticketId = await addTicket(userId);
       const response = await addToBooks(userId, ticketId);
       return response;
    } else {
       throw new Error("Error getting data")
    }
  } catch(error) {
     console.log(error);
  }
}

This doesn't use Promise.all, but since the example you are giving, to my understanding, only needs the userId to complete the other 2 operations, I think this should work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文