D
D
Denis Labutin2018-09-30 00:41:36
Vue.js
Denis Labutin, 2018-09-30 00:41:36

Explain what is happening in this section of JS code?

const _products = [....]; 
let shop = {
    getProducts (cb) {
        setTimeout(() => cb(_products), 100)
    },
};

shop.getProducts(products => {
      commit('setProducts', products)
    })

I need to get a list of products, I don’t understand how it all works.
As I understand it, we pass this function to getProducts
products => {
      commit('setProducts', products)
    }

and _products (constant) is passed to this function and it is executed there???
This is an example from Vuex.
Why is this done?
Is it just because of setTimeout to send this function to the queue?
In a real example, you can't just return.
It is very difficult to understand this bunch of callbacks passing from one function to another. Are there any articles? This is all because of the asynchrony of the code, and how to rebuild?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question