为什么这个承诺没有解决回调用者?

Why is this promise not resolving back to the caller?

我有一个运行 Vuex 和 Axios 的 Vue-App。在这个应用程序中,我有处理 API-calls 的 vuex-store,但问题是,当我调用 store-actions 时,我无法将响应链接到 caller.Any 想法中,我做错了什么?

调用代码:

import { FETCH_PRODUCTS, ADD_PRODUCT } from './actions.type'

methods: {
    sendNewProduct () {
      this.$store
        .dispatch(ADD_PRODUCT, this.newProductForm)
        .then(() => {
          console.log('This never gets called')
        })
    }
  }

Vuex 商店:

const actions = {
  [ADD_PRODUCT] (context, credentials) {
    return new Promise((resolve) => {
      ApiService
        .post('/Products/', {
          Name: credentials.Name,
          Description: credentials.Description,
          Price: credentials.Price
        })
        .then(({ data }) => {
          this.$store
            .dispatch(FETCH_PRODUCTS)
            resolve(data)
        })
        .catch(({ response }) => {
          console.log(response)
          context.commit(SET_ERROR, 'Error adding product')
        })
    })
  }
}
const actions = {
  [ADD_PRODUCT](context, credentials) {
    return ApiService.post("/Products/", {
      Name: credentials.Name,
      Description: credentials.Description,
      Price: credentials.Price
    })
      .then(({ data }) => {
        this.$store.dispatch(FETCH_PRODUCTS);
        return data;
      })
      .catch(({ response }) => {
        console.log(response);
        context.commit(SET_ERROR, "Error adding product");
        throw new Error("Error adding product");
      });
  }
};

我删除了 new Promise(...) 因为 axios 已经创建了一个承诺。 如果在 then 回调中添加一个 return data 并在 catch 回调中添加一个 throw 让调用 api 接收 data/error.

请注意,承诺会在 FETCH_PRODUCTS 完成之前解决,为确保该操作也已完成,您可以这样写:

.then(({ data }) => {
  return this.$store.dispatch(FETCH_PRODUCTS)
    .then(() => data);
})