1

I'm having problem to make my Action resolve the promise. I've read what looks like the most relevant posts.

Returning Promises from Vuex actions

I want to know when my action is finished, then my component can start doing other stuff. The problem is, the Action never returns the Promise.

myComponent.vue

 methods: {
  loadUrls() {
    this.$store.dispatch('getUrls').then(result => {
      console.log("getUrls result", result)
    })
  },

vuex.js

  actions: {
    getUrls() {
      console.log("getUrls")
      return new Promise((resolve) => {
        setTimeout(() => {
          console.log("setTimeout in")
          resolve("Resolved!")
        }, 1000)
      })
    },

That's my console log:

enter image description here

I've used the "setTimeout" to make as simple as possible the problem. In real life I call an API.

I do not need to rely on the result of this promise. I'm aware about it. I use Vuex as the source of truth, but I need to track when the event in completed.

Thanks in advance =)

3rdSenna
  • 344
  • 2
  • 16
  • 1
    I cannot reproduce this. https://codesandbox.io/s/blissful-butterfly-5hw8p?file=/src/main.js – User 28 Jul 18 '20 at 03:26
  • @User28, thanks for that. It helped me to ensure that my code was correct. I've modified your project to double check I was not missing any fundamental VueJs concept, and all worked. https://codesandbox.io/s/boring-nightingale-n24xx The problem was on my environment . I needed to clean and restart everything . – 3rdSenna Jul 18 '20 at 08:38

1 Answers1

0

SOLVED! It worked after I delete my dist Folder, close VSCode and open a new Chrome instance using the new build local host URL.

Thanks @User-28. I saw his shared code and realised nothing was wrong with my code. Then I start looking at my environment.

My very first code didn't have Promise Resolve in the action. I compiled and I was testing it.

Then I found Returning Promises from Vuex actions which explained how to use the Promise in it. I compiled and I was TRYING to test it. Never success. Somehow the code without the Promise was always there. After clean up Dist folder, Close VS code and use a new Chrome instance, the new code was in place and worked. I'm still don't know the actual problem, but at least it can keep going forward now.

3rdSenna
  • 344
  • 2
  • 16