M
M
Michael2020-06-30 08:43:15
Vue.js
Michael, 2020-06-30 08:43:15

Why setTimeout here?

Greetings.
I'm looking at this official example of a shopping cart using vuex, and I don't understand why they use setTimeout?
Here is the code:
shop.js

const _products = [
  {"id": 1, "title": "iPad 4 Mini", "price": 500.01, "inventory": 2},
  {"id": 2, "title": "H&M T-Shirt White", "price": 10.99, "inventory": 10},
  {"id": 3, "title": "Charli XCX - Sucker CD", "price": 19.99, "inventory": 5}
]

export default {
  getProducts (cb) {
    setTimeout(() => cb(_products), 100)       // <--------- Вот этот
  }
}

product.js
import shop from '../../api/shop'

// initial state
const state = () => ({
  all: []
})

// getters
const getters = {}

// actions
const actions = {
  getAllProducts ({ commit }) {
    shop.getProducts(products => {
      commit('setProducts', products)
    })
  }
}

Here is the action in the ProductList.vue component:
export default {
  // ...
  created () {
    this.$store.dispatch('products/getAllProducts')
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Loli E1ON, 2020-06-30
@Nolan81

Emulate delay

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question