Monday, May 20, 2024
12
rated 0 times [  15] [ 3]  / answers: 1 / hits: 47478  / 6 Years ago, thu, july 5, 2018, 12:00:00

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?


More From » ecmascript-6

 Answers
8

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);




[#54048] Monday, July 2, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jayla

Total Points: 14
Total Questions: 96
Total Answers: 123

Location: Greenland
Member since Fri, Jul 31, 2020
4 Years ago
;