Moshe's Blog

Tips and tricks learnt along the way.

Writing Async Javascript Code in Sync Format - Part 2

| Comments

This is a continuation of Writing Async Javascript Code in Sync Format – Part 1

There’s a new es6 feature called Destructuring Assignment that looks like this:

1
2
3
4
5
6
var [a, b] = ['a', 'b'];
a === 'a';
b === 'b';
var {q:q, r:r} = {q: 'q', r: 'r'};
q === 'q';
r === 'r';

Keep in mind that objects can mimic arrays so this works:

1
2
3
var [x,y] = {0: 'x', 1: 'y'};
x === 'x';
y === 'y';

There’s also a shortcut available for assigning to objects in es6 where var {x:x, y:y} can be written as var {x, y} This means we could have written

1
2
3
var {q, r} = {q: 'q', r: 'r'};
q === 'q';
r === 'r';

Using this knowledge and what we did in the first part we can write really cool concurrent code:

Promise.props is a bluebird specific function that does what Promise.all except on objects. If you’re using native Promises it doesn’t take much to polyfill it

Comments