首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >tRPC (在nextjs上)杀死不被期待的承诺

tRPC (在nextjs上)杀死不被期待的承诺
EN

Stack Overflow用户
提问于 2022-01-30 07:51:23
回答 1查看 532关注 0票数 1

我有一个突变,可以改变外部系统中的数据(例如,posts)。

从我的UI中,在external-api上发布公共文章,并在此突变之后获取此api以进行刷新。

我希望保存使我的系统使用extenal-api的更改,但这并不是很重要,更重要的是在上瘾newPost之后立即在我的站点上更新posts,而不是等待将这些帖子保存在我的数据库中。

我原以为这段代码

代码语言:javascript
复制
  .mutation('post.add', {
    input: z.object({ title: z.string(), text: z.string() }),
    async resolve({ input }) {
      const postId = await useExternApiForPublicThisPost(input);
      
      // save change in my database, but not await response
      prisma.posts.create({
        data: {postId, ...input}
      });
    }
  })

但是不幸的是,在解决了这个请求之后,tRPC杀死了Prisma,因为数据库中的保存数据需要等待Prisma,但是速度很慢

如何从tRPC返回响应,但继续执行其余的承诺?

EN

回答 1

Stack Overflow用户

发布于 2022-01-31 10:38:17

普莉丝玛不像我预料的那样工作

代码语言:javascript
复制
      prisma.posts.create({
        data: {postId, ...input}
      });

不要开始承诺,只在调用.then()之后才开始承诺

我不想使用await prisma.post.create(),但可以使用prisma.post.create().then()或prisma.$transaction(prisma.post.create())

初始代码的正确版本。

代码语言:javascript
复制
  .mutation('post.add', {
    input: z.object({ title: z.string(), text: z.string() }),
    async resolve({ input }) {
      const postId = await useExternApiForPublicThisPost(input);
      
      // save change in my database, but not await response
      prisma.posts.create({
        data: {postId, ...input}
      }).then();
    }
  })
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70912842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档