Nub Coder
Nub Coder

Reputation: 25

What does ()=>async()=>{} mean?

I'm confused with that redux thunk action:

import axios from 'axios';

export const GET_CHANNELS = 'GET_CHANNELS'

export const getChannels = () => async (dispatch, getState) => {
    const res = await axios.get('https://v-forum-api.bahdcasts.com/api/channels')
    dispatch({
        type: GET_CHANNELS,
        payload: res.data
    })
}

What does the following construct mean?

const getChannels=()=>async()=>{}

Can you please provide any article link for that expression? Thank you

Upvotes: 0

Views: 403

Answers (2)

F.NiX
F.NiX

Reputation: 1505

const getChannels = () => async() => {}

is somehow equal to:

function getChannels() {
  return async function() {

  }
}

using arrow functions (which changes the usage of this) & getChannels is constant block-leveled variable.

Upvotes: 0

CherryDT
CherryDT

Reputation: 29072

It is a function that returns another (async) function.

Ignoring the differences in semantics of this between arrow functions and regular functions, a possibly clearer way to write the same thing with regular functions would be:

const getChannels = function () {
  return async function (dispatch, getState) {
    // ...
  }
}

The caller would call getChannels() and get a function back, which could then be called as well.

const innerFunction = getChannels()
await innerFunction(dispatch, getState)

Upvotes: 3

Related Questions