Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to yield multiple constants with one declaration

I have a function makeThings(n) that returns a Promise to create n number of items (through Sequelize).

It was suggested to me to use this format for retrieving values:

const [thing1, thing2] = yield makeThings(2);

I can't find anything on the internet about assigning constants/variables in this way. Have you seen this? How does it work? My machine doesn't like it and spits out an unexpected token "[" error.

Maybe it might be a npm package that I don't know about?

Using Node.js 5.1.0, ES6

like image 784
the holla Avatar asked Jun 08 '26 18:06

the holla


1 Answers

This is a thing in ES6. It's called "Destructuring Assignment"

ECMAScript 6

var list = [ 1, 2, 3 ]
var [ a, , b ] = list
[ b, a ] = [ a, b ]

ECMAScript 5

var list = [ 1, 2, 3 ];
var a = list[0], b = list[2];
var tmp = a; a = b; b = tmp;

It's from http://es6-features.org/#ArrayMatching. It's a pretty good site to get introduced to some of the new features in es6.

like image 71
D-- Avatar answered Jun 11 '26 07:06

D--