select: Make promise cancellable.
This commit is contained in:
parent
03ebfaf7ab
commit
67012b6f4b
3 changed files with 36 additions and 23 deletions
49
lib/index.js
49
lib/index.js
|
@ -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
|
||||
//
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue