select: Make promise cancellable.

This commit is contained in:
David Braun 2017-10-15 13:32:40 -04:00
parent 03ebfaf7ab
commit 67012b6f4b
No known key found for this signature in database
GPG key ID: 5694EEC4D129BDCF
3 changed files with 36 additions and 23 deletions

View file

@ -309,31 +309,34 @@ Channel.of = (...values) =>
Channel.isChannel = (arg) =>
prototype.isPrototypeOf(arg)
Channel.select = (...methodPromises) => {
const promise = new Promise((resolve, reject) => {
methodPromises.forEach(async (promise) => {
promise.prethen(() => {
// We've been given a heads-up that this method will complete first so
// cancel the other method calls.
methodPromises.forEach((other) => {
if (other !== promise) {
other.cancel()
}
Channel.select = (...methodPromises) =>
Object.assign(
new Promise((resolve, reject) => {
methodPromises.forEach(async (promise) => {
promise.prethen(() => {
// We've been given a heads-up that this method will complete first so
// cancel the other method calls.
methodPromises.forEach((other) => {
if (other !== promise) {
other.cancel()
}
})
})
try {
await promise
} catch (exception) {
reject(exception)
}
resolve(promise.channel)
})
try {
await promise
} catch (exception) {
reject(exception)
}
resolve(promise.channel)
})
})
return promise
}
}),
{
cancel: () =>
Promise.all(methodPromises.map((promise) => promise.cancel()))
}
)
// functional interface allowing full or partial application
//