Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created March 16, 2020 16:16
Show Gist options
  • Save rwaldron/c0aa88cb32d5ab72dc15b737c79db3f8 to your computer and use it in GitHub Desktop.
Save rwaldron/c0aa88cb32d5ab72dc15b737c79db3f8 to your computer and use it in GitHub Desktop.
const slots = new WeakMap();
Object.defineProperties(globalThis, {
AggregateError: {
value: class AggregateError extends Error {
constructor(iterable, message) {
super(message);
slots.set(this, { errors: Array.from(iterable) });
}
get errors() {
return slots.get(this).errors;
}
},
enumerable: false,
writable: true,
configurable: true
}
});
Object.defineProperties(Promise, {
any: {
value: function any(iterable) {
let C = Object(this);
if (typeof C !== 'function') {
throw new TypeError('Invalid "this"');
}
let identity = x => x;
let resolver = value => Promise.resolve.apply(C, [value]);
let rejecter = value => Promise.reject.apply(C, [value]);
return Promise.all(
Array.from(iterable).map(item => {
let promise = resolver(item);
try {
return promise.then(rejecter, identity);
} catch (error) {
return error;
}
})
).then(errors => {
throw new AggregateError(errors, 'Rejections');
}, identity);
},
enumerable: false,
writable: true,
configurable: true
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment