forked from mxcl/PromiseKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrace.swift
More file actions
60 lines (51 loc) · 1.89 KB
/
race.swift
File metadata and controls
60 lines (51 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@inline(__always)
private func _race<U: Thenable>(_ thenables: [U]) -> Promise<U.T> {
let rp = Promise<U.T>(.pending)
for thenable in thenables {
thenable.pipe(to: rp.box.seal)
}
return rp
}
/**
Resolves with the first resolving promise from a set of promises.
race(promise1, promise2, promise3).then { winner in
//…
}
- Returns: A new promise that resolves when the first promise in the provided promises resolves.
- Warning: If any of the provided promises reject, the returned promise is rejected.
- Warning: aborts if the array is empty.
*/
public func race<U: Thenable>(_ thenables: U...) -> Promise<U.T> {
return _race(thenables)
}
/**
Resolves with the first resolving promise from a set of promises.
race(promise1, promise2, promise3).then { winner in
//…
}
- Returns: A new promise that resolves when the first promise in the provided promises resolves.
- Warning: If any of the provided promises reject, the returned promise is rejected.
- Remark: Returns promise rejected with PMKError.badInput if empty array provided
*/
public func race<U: Thenable>(_ thenables: [U]) -> Promise<U.T> {
guard !thenables.isEmpty else {
return Promise(error: PMKError.badInput)
}
return _race(thenables)
}
/**
Resolves with the first resolving Guarantee from a set of promises.
race(promise1, promise2, promise3).then { winner in
//…
}
- Returns: A new guarantee that resolves when the first promise in the provided promises resolves.
- Warning: If any of the provided promises reject, the returned promise is rejected.
- Remark: Returns promise rejected with PMKError.badInput if empty array provided
*/
public func race<T>(_ guarantees: Guarantee<T>...) -> Guarantee<T> {
let rg = Guarantee<T>(.pending)
for guarantee in guarantees {
guarantee.pipe(to: rg.box.seal)
}
return rg
}