javascript - 如何同步调度一组 Vuex 操作(等待一组稳定,然后调度另一组)

标签 javascript async-await vuex

我有几个返回 axios Promises 的 Vuex 操作。 我想运行操作 X 几次,在这些操作结束后,我想运行操作 Y 几次,这是我尝试过的:

async save() {
  const ingredients = [{ 1 }, { 2 }]
  const ingredients_actions = ingredients.map(async ing => await this.$store.dispatch('saveIngredients', ing))
  const recipes = [{ a }, { b }, { c }]
  const recipes_actions = recipes.map(async recipe => await this.$store.dispatch('saveRecipe', recipe)

  await Promise.all(ingredients_actions)
  await Promise.all(recipes_actions)
}

在控制台的网络选项卡中,我希望看到配料_ Action 调用发生,然后看到食谱_ Action 发生。相反,我看到这些 Action 在各处发生,而且根本不同步。

如何使所有的配料 Action 发生在食谱 Action 之前?欢迎所有建议。

最佳答案

这是预期的吗?

async save() {
  const ingredients = [{ 1 }, { 2 }]
  const ingredients_actions = ingredients.map(async ing => await this.$store.dispatch('saveIngredients', ing))
  await Promise.all(ingredients_actions)

  const recipes = [{ a }, { b }, { c }]
  const recipes_actions = recipes.map(async recipe => await this.$store.dispatch('saveRecipe', recipe)
  await Promise.all(recipes_actions)
}

关于javascript - 如何同步调度一组 Vuex 操作(等待一组稳定,然后调度另一组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61342950/

相关文章:

c# - 合并异步方法的结果并同步返回

javascript - Vue路由器-如何根据用户角色在同一路由路径上加载多个组件?

object - 如果 vue.js 2 上的对象为空,如何添加条件?

javascript - 处理 Cypress 网址重定向

javascript - 谷歌可视化 : Animated Line Graph --incremental rather than all at once?

javascript - 在 Android Chrome 中运行 JavaScript 控制台(无需电脑)

javascript - 如何在 O(1) 中更新 redux/vuex 存储中的元素?

javascript - 如何在 React 中显示来自 vimeo 播放器的视频的 iframe?

javascript - 在 Angular 中执行前一个响应后执行一个函数

c# - 如何在同步代码.NET 4.5 中等待所有并发任务完成?