Last active
October 31, 2016 15:38
-
-
Save larryjang/8bb5772836f00aa968ad3829a6e142ff to your computer and use it in GitHub Desktop.
Inverse of ES6 Promise.all
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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