-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from roc-lang/task-result
Add `Task.result`
- Loading branch information
Showing
5 changed files
with
97 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/expect | ||
|
||
# uncomment line below for debugging | ||
# exp_internal 1 | ||
|
||
set timeout 7 | ||
|
||
spawn $env(EXAMPLES_DIR)result | ||
|
||
expect "Listening on localhost port 8000\r\n" { | ||
set curlOutput [exec curl -sS localhost:8000 -d ""] | ||
|
||
if {$curlOutput eq "GOOD"} { | ||
exit 0 | ||
} else { | ||
puts "Error: curl output was different than expected: $curlOutput" | ||
exit 1 | ||
} | ||
} | ||
|
||
puts stderr "\nError: output was different than expected." | ||
exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
app "result" | ||
packages { pf: "../platform/main.roc" } | ||
imports [ | ||
pf.Task.{ Task }, | ||
pf.Http.{ Request, Response }, | ||
] | ||
provides [main] to pf | ||
|
||
# This example demonstrates the use of `Task.result`. | ||
# It transforms a task that can either succeed with `ok`, or fail with `err`, into | ||
# a task that succeeds with `Result ok err`. | ||
|
||
main : Request -> Task Response [] | ||
main = \_ -> | ||
when checkFile "good" |> Task.result! is | ||
Ok Good -> Task.ok { status: 200, headers: [], body: Str.toUtf8 "GOOD" } | ||
Ok Bad -> Task.ok { status: 200, headers: [], body: Str.toUtf8 "BAD" } | ||
Err IOError -> Task.ok { status: 500, headers: [], body: Str.toUtf8 "ERROR: IoError when executing checkFile." } | ||
|
||
checkFile : Str -> Task [Good, Bad] [IOError] | ||
checkFile = \str -> | ||
if str == "good" then | ||
Task.ok Good | ||
else if str == "bad" then | ||
Task.ok Bad | ||
else | ||
Task.err IOError |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters