Skip to content

Instantly share code, notes, and snippets.

@onildoaguiar
Created February 27, 2018 20:22
Show Gist options
  • Save onildoaguiar/c864e179955a3de01f6ea671ad463f88 to your computer and use it in GitHub Desktop.
Save onildoaguiar/c864e179955a3de01f6ea671ad463f88 to your computer and use it in GitHub Desktop.
Example of Promise.all with destructuring
'use strict';

const callMock = (object) => new Promise((resolve) => setTimeout(() => resolve(object), 500));

const print = (method, petOne, petTwo) => {
	console.log(`Method: ${method}`);
	console.log(`PetOne: ${JSON.stringify(petOne)}`);
	console.log(`PetTwo: ${JSON.stringify(petTwo)}`);
};

// async / await
const asyncCall = async () => {
	const [petOne, petTwo] = await Promise.all([callMock({ pet: 'dog' }), callMock({ pet: 'cat' })]);
	print('async / await', petOne, petTwo);
};

asyncCall();

// then
Promise.all([callMock({ pet: 'dog' }), callMock({ pet: 'cat' })])
	.then(([petOne, petTwo]) => print('then', petOne, petTwo));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment