DEV Community

[Comment from a deleted post]
Collapse
 
nicorafales profile image
Nicolás Rafales • Edited

meh

const getProduct = a => b => 
    new Promise((resolve, reject) => setTimeout(() => resolve(a * b), 1000))
;

const printFinalResult = result => console.log('final result', result);

// execute

getProduct(2)(4)
    .then(getProduct(2))
    .then(printFinalResult)
    .catch(console.error)
;

Enter fullscreen mode Exit fullscreen mode

Altough I did find it helpful.
I think ppl that don't like the FP flow might find the thenables more complicated. For me it's the other way around.

notes: The technique of returning a function from a function is called higher order function, which allows you to do a partial-application of a function, something very common on FP languages/frameworks/libraries. The extent of this (when used up until the last parameter) is called currying.

Collapse
 
myogeshchavan97 profile image
Yogesh Chavan • Edited

While using this way also works, we should not un-necessarily return a function from another function as the inner function will remain in the memory occupying space even after the outer function has finished executing.

Collapse
 
nicorafales profile image
Nicolás Rafales • Edited

could you provide some feedback on this that demonstrates this is a memory leak?

What happens when we pass a "callback" to an Array.prototype.filter|map|reduce ?

What happens when we use libraries like lodash|lodash FP|Rambda and most of the frameworks we use that jump into callbacks from callbacks?

how do they handle those memory leaks? or do they not?

 
myogeshchavan97 profile image
Yogesh Chavan • Edited

You can check out this article to better understand how memory leak can happen when using closures. In filter, map or reduce we provide a callback function as an argument. We don't return a function from a function there.