-3

Is it possible to return multiple values from async/await in nodejs. For e.g I can do this in NodeJs Callback as below:

myValues("username1", function(err, country, pincode){
    console.log(country);
    console.log(pincode);
});

function myValues(value1, callback){
    var country = getValue2FromAPI(value1); //foo
    var pincode = getValue3FromAPI(value1); //bar
    callback(null, country, pincode);
}

In async/await I would write it as:

var final = await myValues("username1");
async function myValues(value1){
   var country = getValue2FromAPI(value1);
   var pincode = getValue3FromAPI(value1); 
   return country;  // I would want to return country and pincode both
}
Aditya Mertia
  • 495
  • 7
  • 19
  • 3
    There's nothing asynchronous about `myValues()` so this isn't asynchronous. For it to be asynchronous you would be waiting for some I/O (Timers, Network Requests, File Interactions, DB Requests, etc...). Additionally, if you want to return multiple values you should return them in either an Object or an Array if you plan to make this asynchronous. You can read more about how you can return from Asynchronous calls from [this answer](https://stackoverflow.com/a/14220323/2980607). – peteb Feb 09 '18 at 15:32
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – peteb Feb 09 '18 at 15:32
  • for simplicity i didn't write it as I/O task but yes what you said is correct, the assumption for this question is to have I/O tasks. So from what you said there is no way to return multiple values as in callbacks. Only way is to pass it as arrays or objects. – Aditya Mertia Feb 09 '18 at 15:38

1 Answers1

3

If that is what you want:

return value2;  // I would want to return value1 and value2 both

then return an array:

return [value1, value2];

or an object:

return { value1, value2 };

What async/await can return (which, by the way, is not what you have in your examples, despite the title of your question) is the same as what a promise can be resolved to, which is always a single value - but that single value can be an array or an object.

The problem that you describe, i.e. a callback function getting more than one non-error arguments and promises being resolved with a single value is an impedance mismatch between traditional Node-style error-first callbacks and promises (or async/await). This is demonstrated on the example of modules like request-promise:

that promisify callback-style modules like request:

that sometime allow for more than one value passed as callback arguments.

This is by the way caused by an asymmetry between function arguments and return statements. Some languages support only single-argument functions - sometimes with convenient syntax so you don't even know about it - and some languages support multiple values returned from functions. JavaScript supports multiple arguments but single returned values so what can be passed as arguments is not the same as what can be returned (unless you treat all arguments as a single array, that is).

rsp
  • 107,747
  • 29
  • 201
  • 177