LoginSignup
18
19

More than 3 years have passed since last update.

Vuexでasync/awaitを用いて値を返すアクションを記述する

Last updated at Posted at 2019-03-15

何を今更ということかもしれないけれど、自分にとってかなりの:sunrise:新しい地平が開けた感:sunrise:があったので一応記事として残しておくことにしました。

そもそもVuexのアクションとは?

アクションは、状態を変更するのではなく、ミューテーションをコミットします。
アクションは任意の非同期処理を含むことができます。

自分もアクションはミューテーションをコミットしたりするものだと思ってました(その通りです)。
ユーザが何か操作すると、それをトリガーにアクション、ミューテーション、ステートと処理が伝播していき、ステートの変化がUIに反映されたりして、ユーザに伝わるというのが通常のVuexの流れだと思います。
そのようにVuexを書いていたあるとき、Vuexの複数のアクションの処理が抽象化出来そうな状況が生まれました:eyes:
そこでふと立ち止まってしまいました。アクションの返り値は何なのだと:thinking:

抽象化出来そうなアクション

そこで、CodeSandboxで以下のようなVuexコードを試してみました:pencil:

import Vue from "vue";
import App from "./App.vue";
import Vuex from "vuex";

Vue.config.productionTip = false;

Vue.use(Vuex);

const store = {
  state: {
    text: "hello"
  },
  actions: {
    doubleText({ state }) {
      let newText = [];
      for (let i = 0; i < 2; i++) {
        newText.push(state.text);
      }
      newText = newText.join("!! ") + "!!";
      console.log(newText);
    },
    tripleText({ state }) {
      let newText = [];
      for (let i = 0; i < 3; i++) {
        newText.push(state.text);
      }
      newText = newText.join("!! ") + "!!";
      console.log(newText);
    }
  }
};

new Vue({
  render: h => h(App),
  store: new Vuex.Store(store),
  mounted() {
    this.$store.dispatch("doubleText");
    this.$store.dispatch("tripleText");
  }
}).$mount("#app");

2つのアクション内で、文字列をループで回して増やしている部分を共通化出来そうです。

import Vue from "vue";
import App from "./App.vue";
import Vuex from "vuex";

Vue.config.productionTip = false;

Vue.use(Vuex);

const store = {
  state: {
    text: "hello"
  },
  actions: {
    multiText({ state }, num) {
      let newText = [];
      for (let i = 0; i < num; i++) {
        newText.push(state.text);
      }
      return newText.join("!! ") + "!!";
    },
    doubleText({ dispatch }) {
      const text = dispatch('multiText', 2);
      console.log(text);
    },
    tripleText({ dispatch }) {
      const text = dispatch('multiText', 3);
      console.log(text);
    }
  }
};

new Vue({
  render: h => h(App),
  store: new Vuex.Store(store),
  mounted() {
    this.$store.dispatch("doubleText");
    this.$store.dispatch("tripleText");
  }
}).$mount("#app");

単純にまとめてコンソールを確認すると、Promiseが返ってきました。
な、なるほどーー :flushed:

async/awaitを用いたアクション

ということは、async/awaitが使えるということで、
実はこれは公式ドキュメントにも書かれています:thumbsup:

import Vue from "vue";
import App from "./App.vue";
import Vuex from "vuex";

Vue.config.productionTip = false;

Vue.use(Vuex);

const store = {
  state: {
    text: "hello"
  },
  actions: {
    multiText({ state }, num) {
      let newText = [];
      for (let i = 0; i < num; i++) {
        newText.push(state.text);
      }
      return newText.join("!! ") + "!!";
    },
    async doubleText({ dispatch }) {
      const text = await dispatch('multiText', 2);
      console.log(text);
    },
    async tripleText({ dispatch }) {
      const text = await dispatch('multiText', 3);
      console.log(text);
    }
  }
};

new Vue({
  render: h => h(App),
  store: new Vuex.Store(store),
  mounted() {
    this.$store.dispatch("doubleText");
    this.$store.dispatch("tripleText");
  }
}).$mount("#app");

そこに書いていいのかという疑問はありつつも、これでアクションに普通の関数(のようなもの)が書けました:thumbsup:
ひとつ賢くなりました:raised_hands::two_hearts::beers::sushi::kissing_heart:

18
19
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
19