Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created January 23, 2016 20:08
Show Gist options
  • Save vlad-bezden/99f033581bedb4e951d9 to your computer and use it in GitHub Desktop.
Save vlad-bezden/99f033581bedb4e951d9 to your computer and use it in GitHub Desktop.
Parallel Promises

Parallel Promises

Example on how to use in parallel Promises and get value after tasks are completed. Example is using expect library for assertion

A Pen by Vlad Bezden on CodePen.

License.

'use strict';
/**
* Increment a given value
* @param {Number} val
* @returns {Promise}
*/
const foo = (val) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(val + 1);
}, 100);
});
},
/**
* Increment a given value
* @param {Number} val
* @returns {Promise}
*/
bar = (val) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(val + 2);
}, 200);
});
};
Promise.all([foo(1), bar(2)]).then((arr) => {
expect(arr).toInclude[2, 4];
expect(arr.length).toEqual(2);
});
console.log('All Tests Passed!');
<script src="https://npmcdn.com/expect/umd/expect.min.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment