Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chained Arrow function syntax

const fetch = url => dispatch => {
  // ...
}

export const fetchQuestions = tag => (dispatch) => {
  return dispatch(fetch(tag));
};

What is dispatch in the fetch function ? url is a first and single parameter fetch function. But what is dispatch here ?

like image 561
ceth Avatar asked Jul 17 '17 14:07

ceth


2 Answers

This is equivalent to one function returning another. I.e. this

const fetch = url => dispatch => {
    // ...
}

is equivalent to

const fetch = function(url) {
    return function(dispatch) {
        // ... 
    }
}

Similarly this

export const fetchQuestions = tag => (dispatch) => {
  return dispatch(fetch(tag));
};

is equivalent to

export const fetchQuestions = function(tag) {
    return function(dispatch) {
        return dispatch(fetch(tag));
    }
};
like image 70
Aron Avatar answered Sep 23 '22 02:09

Aron


dispatch is the first and single parameter of the function returned by the url => ... function. With normal function syntax, it would be

const fetch = function(url) {
    return function(dispatch) {...}
}
like image 24
baao Avatar answered Sep 27 '22 02:09

baao