multiple return values... like ruby

Nathan Wall nathan.wall at live.com
Thu Jan 3 21:51:05 PST 2013


Anton Kovalyov wrote:
> anders elo wrote:
>> I'd like to propose the incorporation of multiple return values into 
>> the ES standard. 
>> 
>> function foo(){ 
>> return 1,2,3; 
>>>> 
>> let (a,b,c) = foo(); 
>> 
>> let (a,b) = foo(); // ignore c 
>> 
>> let (a,...b) = foo() // a = 1, b = [2,3] 
>> 
>> /* also useful for asynchronous functions returning a promise */ 
>> let (response,headers) = yield getAjaxData("http://some.host.com/data"); 
>
> See here: http://wiki.ecmascript.org/doku.php?id=harmony:destructuring 
> and 
> https://developer.mozilla.org/en-US/docs/JavaScript/New_in_JavaScript/1.7#Destructuring_assignment_%28Merge_into_own_page.2Fsection%29 
> 
> Anton 

Hi Anders,

The syntax you proposed is not backwards compatible.

Anton has directed you to articles on destructuring, which is the ES6 way to accomplish what you want.

Let me elaborate. Your examples can be accomplished as follows:

    function foo() {
        // We return an array of values.
        return [ 1, 2, 3 ];
    }

    let [ a, b, c ] = foo();

    let [ a, b ] = foo(); // c is ignored

    let [ a, ...b ]  = foo(); // a is 1, b is [ 2, 3 ]

I believe if you are using Task.js, you can additionally use the asynchronous form in like manner.

Nathan 		 	   		  


More information about the es-discuss mailing list