18

I know this is a duplicated question with ES5, but I am looking for the syntax with ES6 arrow function. My code below:

fetchItems = (callback) => {
    //After ajax success
    callback(response);
}

const myParams = {name:"John"}
this.fetchItems((res) => {
    console.log(res.data);
});

For the above scenario, I want to pass some parameters(myParams) along with the function call, how can I achieve that?

Scott Baker
  • 10,013
  • 17
  • 56
  • 102
Jaison James
  • 4,414
  • 4
  • 42
  • 54

2 Answers2

12

You can do that:

const fetchItems = (callback, ...params) => {
    //Do whatever you want with the params
    callback(response);
}

Example of usage:

const fetchItems = (callback, ...params) => {
    callback(params);
}
    
fetchItems (console.log, 'foo', 1);
Guerric P
  • 30,447
  • 6
  • 48
  • 86
4

More of less you can do it the same way

const getCountries = (data, callback) => {
    //After ajax success
    callback(response);
}

getCountries("data", ()=>{});
void
  • 36,090
  • 8
  • 62
  • 107