Vuejs callback problem?

in the study of the vuex project, I found someone else"s code from github. Can the "callback (_ products)" here be used in this way? I don"t understand this way of writing

.
/**
 * 
 */

var _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:function(callback){
        //callback
        setTimeout(callback(_products), 300)
    },

}

callback if it is return function (products) {/ * * /}, there should be no problem.


300ms simulates the acquisition of backend data, usually when it is not in conjunction with the backend. Self-compiled data to see whether the program runs as expected


it is semantically wrong to execute callback and then setimeout unless he executes the callback method to return a new method


timer does not seem to be correct.
this will be executed immediately, not after 300ms. Try changing 10000 to 10000 to see if it works


callback is a function, _ products is a global variable, callback (_ products) is just a callback function.


Let's put it this way.
here _ products is a set of data.
when you call the getProducts () method. To get data in cb .

getProducts(res => {
    // ?
    // res _products
});

setimeout is to better simulate the background request situation in 300ms before res can get the value.

Menu