demas demas - 15 hours ago 2
Javascript Question

Syntax of arrow functions

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 ?

Answer

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