mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-04 12:18:19 +00:00
Add Task.combine
This commit is contained in:
parent
57cab7f69a
commit
2b6eaf48d8
2 changed files with 30 additions and 5 deletions
|
@ -11,6 +11,7 @@ module [
|
|||
loop,
|
||||
fromResult,
|
||||
batch,
|
||||
combine,
|
||||
sequence,
|
||||
forEach,
|
||||
result,
|
||||
|
@ -187,8 +188,11 @@ fromResult : Result a b -> Task a b
|
|||
fromResult = \res ->
|
||||
@Task \{} -> res
|
||||
|
||||
## Apply a task to another task applicatively. This can be used with
|
||||
## [ok] to build a [Task] that returns a record.
|
||||
## Apply a task to another task applicatively.
|
||||
##
|
||||
## DEPRECATED: Modern record builders use [combine].
|
||||
##
|
||||
## This can be used with [ok] to build a [Task] that returns a record.
|
||||
##
|
||||
## The following example returns a Record with two fields, `apples` and
|
||||
## `oranges`, each of which is a `List Str`. If it fails it returns the tag
|
||||
|
@ -207,6 +211,26 @@ batch = \current ->
|
|||
await next \f ->
|
||||
map current f
|
||||
|
||||
## Combine the values of two tasks with a custom combining function.
|
||||
##
|
||||
## This is primarily used with record builders.
|
||||
##
|
||||
## ```
|
||||
## { a, b, c } =
|
||||
## { Task.combine <-
|
||||
## a: Task.ok 123,
|
||||
## b: File.read "file.txt",
|
||||
## c: Http.get "http://api.com/",
|
||||
## }!
|
||||
## ```
|
||||
combine : Task a err, Task b err, (a, b -> c) -> Task c err
|
||||
combine = \@Task leftTask, @Task rightTask, combiner ->
|
||||
@Task \{} ->
|
||||
left = try leftTask {}
|
||||
right = try rightTask {}
|
||||
|
||||
Ok (combiner left right)
|
||||
|
||||
## Apply each task in a list sequentially, and return a list of the resulting values.
|
||||
## Each task will be awaited before beginning the next task.
|
||||
##
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue