'Curries arrow function in JS

Please explain this function in simple format(without arrow use) and I am confused in promise.resolve "foo" is argument or a callback function

module.exports= foo =>(req,res,next)=>
Promise.resolve(foo(req,res,next)).catch(err=>next(err))


Solution 1:[1]

I'm assuming you already know what Promise.resolve does, so I'll just explain the foo part.

Ah heck, why not just explain everything.

Promise.resolve returns a promise that immediately resolves with the value given. For instance, if you did:

Promise.resolve(2 * 2)
.then(function(value) {
    console.log('Received value', value);
});

you'd instantly see a console message saying Received value 4

foo(req, res, next)

foo is a function that will do something using the three callback functions: req, res, and next

So what this does:

Promise.resolve(foo(req, res, next)).catch(err => next(err))

rewritten without arrow functions:

Promise.resolve(foo(req, res, next))
.catch(function(err) {
    return next(err)
});

is that it takes the 'result' of this function call

foo(req, res, next)

and sends it to the Promise.resolve function, which will return a Promise that resolves to that said 'result'.

The .catch method will catch any errors that occurred in the foo function call and pass it to the next callback to handle it.

Hope this clarifies any doubts you have.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Praise Dare