Michael Michael - 4 months ago 17
Javascript Question

Pass multiple parameters within Promises

I want to pass the previously resolved, returned data and an additional parameter within a promise chain. See the example for clarification.

Below functions both return a Promise and are properly executed. It's really just about passing additional parameter.

Lets consider a Promise chain like:

API.getSomething(id).then(API.processIt)


getSomething function(id) { returns a promise with data }

processIt function(data) { process the returned data }


With a syntax like above it works fine. Once I add additional parameter:

API.getSomething(id).then(API.processIt(data, "random"))

processIt function(data, misc) {...} it does't work anymore.


Is there a way to pass additional parameters within a Promise chain using the result of the previous executed Promise without any additional library?

It's not about the design of the whole chain. I know, the problem could be bypassed with a different design but due to changes in some APIs that's the way I have to handle with the problem.

Answer

On this line

API.getSomething(id).then(API.processIt(data, "random"))

You are trying to pass function as reference but you are invoking the function instead.

Try

API.getSomething(id).then(function(data){
    API.processIt(data, "random");
});