Skip to content

Commit 1de1b5c

Browse files
committed
feat(await): added async versions of all methods
1 parent 2594a17 commit 1de1b5c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

await/await.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ func MustAll(awaitables ...*task.Task) []interface{} {
3939
return v
4040
}
4141

42+
// AllAsync does the same thing as All but does so asynchronously
43+
func AllAsync(awaitables ...*task.Task) *task.Task {
44+
return task.New(func(t *task.Internal) {
45+
t.Resolve(MustAll(awaitables...))
46+
})
47+
}
48+
4249
// AllOrError will wait for every given task to emit a result or
4350
// return as soon as an error is encountered, stopping all other tasks.
4451
func AllOrError(awaitables ...*task.Task) ([]interface{}, error) {
@@ -88,6 +95,13 @@ func MustAllOrError(awaitables ...*task.Task) []interface{} {
8895
return v
8996
}
9097

98+
// AllOrErrorAsync does the same thing as AllOrError but does so asynchronously
99+
func AllOrErrorAsync(awaitables ...*task.Task) *task.Task {
100+
return task.New(func(t *task.Internal) {
101+
t.Resolve(MustAllOrError(awaitables...))
102+
})
103+
}
104+
91105
// AllOrErrorWithTimeout does the same as AllOrError but allows you to set a
92106
// timeout for waiting for other tasks to stop.
93107
func AllOrErrorWithTimeout(timeout time.Duration, awaitables ...*task.Task) ([]interface{}, error) {
@@ -137,6 +151,13 @@ func MustAllOrErrorWithTimeout(timeout time.Duration, awaitables ...*task.Task)
137151
return v
138152
}
139153

154+
// AllOrErrorWithTimeoutAsync does the same thing as AllOrErrorWithTimeout but does so asynchronously
155+
func AllOrErrorWithTimeoutAsync(timeout time.Duration, awaitables ...*task.Task) *task.Task {
156+
return task.New(func(t *task.Internal) {
157+
t.Resolve(MustAllOrErrorWithTimeout(timeout, awaitables...))
158+
})
159+
}
160+
140161
// FastAllOrError does the same as AllOrError but does not wait for all other
141162
// tasks to stop, it does tell them to stop it just doesn't wait for them to stop.
142163
func FastAllOrError(awaitables ...*task.Task) ([]interface{}, error) {
@@ -186,6 +207,13 @@ func MustFastAllOrError(awaitables ...*task.Task) []interface{} {
186207
return v
187208
}
188209

210+
// FastAllOrErrorAsync does the same thing as FastAllOrError but does so asynchronously
211+
func FastAllOrErrorAsync(awaitables ...*task.Task) *task.Task {
212+
return task.New(func(t *task.Internal) {
213+
t.Resolve(MustFastAllOrError(awaitables...))
214+
})
215+
}
216+
189217
// Any will wait for the first task to emit a result (or an error)
190218
// and return that, stopping all other tasks.
191219
func Any(awaitables ...*task.Task) (interface{}, error) {
@@ -220,6 +248,13 @@ func MustAny(awaitables ...*task.Task) interface{} {
220248
return v
221249
}
222250

251+
// AnyAsync does the same thing as Any but does so asynchronously
252+
func AnyAsync(awaitables ...*task.Task) *task.Task {
253+
return task.New(func(t *task.Internal) {
254+
t.Resolve(MustAny(awaitables...))
255+
})
256+
}
257+
223258
// AnyWithTimeout does the same as Any but allows you to set a
224259
// timeout for waiting for other tasks to stop.
225260
func AnyWithTimeout(timeout time.Duration, awaitables ...*task.Task) (interface{}, error) {
@@ -254,6 +289,13 @@ func MustAnyWithTimeout(timeout time.Duration, awaitables ...*task.Task) interfa
254289
return v
255290
}
256291

292+
// AnyWithTimeoutAsync does the same thing as AnyWithTimeout but does so asynchronously
293+
func AnyWithTimeoutAsync(timeout time.Duration, awaitables ...*task.Task) *task.Task {
294+
return task.New(func(t *task.Internal) {
295+
t.Resolve(MustAnyWithTimeout(timeout, awaitables...))
296+
})
297+
}
298+
257299
// FastAny does the same as Any but does not wait for all other tasks to stop,
258300
// it does tell them to stop it just doesn't wait for them to stop.
259301
func FastAny(awaitables ...*task.Task) (interface{}, error) {
@@ -288,6 +330,13 @@ func MustFastAny(awaitables ...*task.Task) interface{} {
288330
return v
289331
}
290332

333+
// FastAnyAsync does the same thing as FastAny but does so asynchronously
334+
func FastAnyAsync(awaitables ...*task.Task) *task.Task {
335+
return task.New(func(t *task.Internal) {
336+
t.Resolve(MustFastAny(awaitables...))
337+
})
338+
}
339+
291340
// ErrTaskFailed is returned by the All methods when at least one task returns an error.
292341
type ErrTaskFailed struct {
293342
Errors []error

0 commit comments

Comments
 (0)