Skip to content

Instantly share code, notes, and snippets.

@larryjang
Last active October 31, 2016 15:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larryjang/8bb5772836f00aa968ad3829a6e142ff to your computer and use it in GitHub Desktop.
Save larryjang/8bb5772836f00aa968ad3829a6e142ff to your computer and use it in GitHub Desktop.
Inverse of ES6 Promise.all
// Inspired by Jordan Harband(https://github.com/ljharb)
let user1 = { role: 'A'};
let user2 = { role: 'B'};
let user3 = { role: 'C'};
const checkA = x => {
return new Promise ( function (resolve, reject) {
if (x.role === 'A') {
return resolve("CheckA resolved");
} else {
return reject("CheckA rejected");
}
});
};
const checkB = x => {
return new Promise ( function (resolve, reject) {
if (x.role === 'B') {
return resolve("CheckB resolved");
} else {
return reject("CheckB rejected");
}
});
};
function antiAll(promises) {
const thrower = x => { throw x; };
const identity = x => x;
return Promise.all(promises.map(x => x.then(thrower, identity)))
.then(thrower, identity);
}
antiAll([checkA(user1), checkB(user1)])
.then((res) => {
console.log(res);
})
.catch( (err) => {
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment