Order of Execution in Function currying and composition — In context of asynchronous operation

Sai Prasanna
Nov 29, 2015 · 4 min read
export default function promiseMiddleware() {
return next => action => {
const { promise, type, ...rest } = action;

if (!promise) return next(action);

const SUCCESS = type;
const REQUEST = type + '_REQUEST';
const FAILURE = type + '_FAILURE';
next({ ...rest, type: REQUEST }); return promise
.then(res => {
next({ ...rest, res, type: SUCCESS });

return true;
})
.catch(error => {
next({ ...rest, error, type: FAILURE });

// Another benefit is being able to log all failures here
console.log(error);
return false;
});
};
}
Middleware1(Middleware2(…))
 applyMiddleware(PromiseMiddleware, LoggerMiddleware) 
PromiseMiddleware(LoggerMiddleware)

But after meditating on the redux source code ,creating curried functions and composing them , I got it.

next({ ...rest, res, type: SUCCESS });

We are first creating a curried function that takes another function as input and also some params.

var asyncFunction = nextFunction => params => window.setTimeout(         function() { 
alert("In Async Callback");
nextFunction(params);
}, 1000);

Now let us create a function to pass into async

var alertFunction = parameters => alert(parameter);

COMPOSING Alert and asyncFunction manually, and calling the result of composition


var composedFunction = asyncFunction(alertFunction);
composedFuntion(1);
Alert:- In Async Callback
Alert:- 1
function params => window.setTimeout(function() { 
alert(“In Async Handler”);
// nextFunction(params) becomes
alert(params);
}, 1000)

Eager to hear your experience in learning functional programming inception.

Sai Prasanna

Written by

Software Engineer, Guitar newbie, Informal Philosopher

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade