Skip to content

Instantly share code, notes, and snippets.

@balinterdi
Last active February 16, 2022 12:26
Show Gist options
  • Save balinterdi/a53b747db009c3cbe52acaf202507f64 to your computer and use it in GitHub Desktop.
Save balinterdi/a53b747db009c3cbe52acaf202507f64 to your computer and use it in GitHub Desktop.
Reimplementing RSVP.hash and RSVP.hashSettled with native promise functions
import {
promiseHash,
promiseHashSettled,
} from 'stock-ticker/utils/promise-utils';
import { module, test } from 'qunit';
module('Unit | Utility | promise-utils', function () {
module('#promiseHash', function () {
test('It works when all promises resolve', function (assert) {
assert.expect(2);
let done = assert.async();
let knightPromise = Promise.resolve(['b1', 'g1', 'b8', 'g8']);
let bishopsPromise = Promise.resolve(['c1', 'f1', 'c8', 'f8']);
let promise = promiseHash({
knights: knightPromise,
bishops: bishopsPromise,
});
promise.then((hash) => {
assert.deepEqual(hash.knights, ['b1', 'g1', 'b8', 'g8']);
assert.deepEqual(hash.bishops, ['c1', 'f1', 'c8', 'f8']);
done();
});
});
test('It works when one of the promises fail', function (assert) {
assert.expect(1);
let done = assert.async();
let knightPromise = Promise.resolve(['b1', 'g1', 'b8', 'g8']);
let bishopsPromise = Promise.reject('The bishops have run away.');
let promise = promiseHash({
knights: knightPromise,
bishops: bishopsPromise,
});
promise.catch((error) => {
assert.strictEqual(error, 'The bishops have run away.');
done();
});
});
});
module('#promiseHashSettled', function () {
test('It works when all promises resolve', function (assert) {
assert.expect(2);
let done = assert.async();
let knightPromise = Promise.resolve(['b1', 'g1', 'b8', 'g8']);
let bishopsPromise = Promise.resolve(['c1', 'f1', 'c8', 'f8']);
let promise = promiseHashSettled({
knights: knightPromise,
bishops: bishopsPromise,
});
promise.then((hash) => {
let { knights, bishops } = hash;
assert.deepEqual(knights.value, ['b1', 'g1', 'b8', 'g8']);
assert.deepEqual(bishops.value, ['c1', 'f1', 'c8', 'f8']);
done();
});
});
test('It works when one of the promises fail', function (assert) {
assert.expect(2);
let done = assert.async();
let knightPromise = Promise.resolve(['b1', 'g1', 'b8', 'g8']);
let bishopsPromise = Promise.reject('The bishops have run away.');
let promise = promiseHashSettled({
knights: knightPromise,
bishops: bishopsPromise,
});
promise.then((hash) => {
let { knights, bishops } = hash;
assert.deepEqual(knights.value, ['b1', 'g1', 'b8', 'g8']);
assert.strictEqual(bishops.reason, 'The bishops have run away.');
done();
});
});
});
});
export function promiseHash(object) {
let map = new WeakMap();
for (let [key, promise] of Object.entries(object)) {
map.set(promise, key);
}
let promises = Object.values(object);
return Promise.all(promises).then((results) => {
let hash = {};
for (let i = 0; i < promises.length; i++) {
let key = map.get(promises[i]);
hash[key] = results[i];
}
return hash;
});
}
export function promiseHashSettled(object) {
let map = new WeakMap();
for (let [key, promise] of Object.entries(object)) {
map.set(promise, key);
}
let promises = Object.values(object);
return Promise.allSettled(promises).then((results) => {
let hash = {};
for (let i = 0; i < promises.length; i++) {
let key = map.get(promises[i]);
hash[key] = results[i];
}
return hash;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment