Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you properly return multiple values from a Promise?

I've recently run into a certain situation a couple of times, which I didn't know how to solve properly. Assume the following code:

somethingAsync()   .then( afterSomething )   .then( afterSomethingElse )    function afterSomething( amazingData ) {   return processAsync( amazingData ); } function afterSomethingElse( processedData ) { } 

Now a situation might arise where I would want to have access to amazingData in afterSomethingElse.

One obvious solution would be to return an array or a hash from afterSomething, because, well, you can only return one value from a function. But I'm wondering if there is a way to have afterSomethingElse accept 2 parameters and invoke it likewise, as that seems a lot easier to document and understand.

I'm only wondering about this possibility since there is Q.spread, which does something similar to what I want.

like image 398
Oliver Salzburg Avatar asked Feb 24 '15 18:02

Oliver Salzburg


People also ask

Can a promise return multiple times?

No. It is not safe to resolve/reject promise multiple times. It is basically a bug, that is hard to catch, becasue it can be not always reproducible.

How can we return multiple values from a function?

We can return more than one values from a function by using the method called “call by address”, or “call by reference”. In the invoker function, we will use two variables to store the results, and the function will take pointer type data. So we have to pass the address of the data.

Can we return multiple values from a function in JavaScript?

Summary. JavaScript doesn't support functions that return multiple values. However, you can wrap multiple values into an array or an object and return the array or the object. Use destructuring assignment syntax to unpack values from the array, or properties from objects.


2 Answers

You can't resolve a promise with multiple properties just like you can't return multiple values from a function. A promise conceptually represents a value over time so while you can represent composite values you can't put multiple values in a promise.

A promise inherently resolves with a single value - this is part of how Q works, how the Promises/A+ spec works and how the abstraction works.

The closest you can get is use Q.spread and return arrays or use ES6 destructuring if it's supported or you're willing to use a transpilation tool like BabelJS.

As for passing context down a promise chain please refer to Bergi's excellent canonical on that.

like image 159
Benjamin Gruenbaum Avatar answered Sep 19 '22 13:09

Benjamin Gruenbaum


you can only pass one value, but it can be an array with multiples values within, as example:

function step1(){   let server = "myserver.com";   let data = "so much data, very impresive";   return Promise.resolve([server, data]); } 

on the other side, you can use the destructuring expression for ES2015 to get the individual values.

function step2([server, data]){   console.log(server); // print "myserver.com"   console.log(data);   // print "so much data, very impresive"   return Promise.resolve("done"); } 

to call both promise, chaining them:

step1() .then(step2) .then((msg)=>{   console.log(msg); // print "done" }) 
like image 44
Alejandro Silva Avatar answered Sep 21 '22 13:09

Alejandro Silva