19

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?

5
  • 2
    What is getCountries what is this.fetchItems and how should they be related?! Commented Jul 5, 2018 at 11:44
  • Sorry, that was a typo Commented Jul 5, 2018 at 11:50
  • As there is no difference between arrow and regular functions in that regard, I'm voting to close this. Commented Jul 5, 2018 at 11:53
  • @JonasW. I guess the question is about "some parameters", since arguments does not work in lambda functions Commented Jul 5, 2018 at 12:03
  • @Jonas W Now I realized that but some scenario it was not worked for me. Commented Jul 5, 2018 at 12:21

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

4

More of less you can do it the same way

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

getCountries("data", ()=>{});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.