Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/src/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Task<A> implements MonadCatchOps<Task, A> {

static Task<A> failed<A>(Object err) => new Task(() => new Future.error(err));

static Task<A> fromEither<A>(Either<Object, A> e) => e.fold((l) => Task.failed<A>(l), (r) => Task.value(r));

static Task<void> sleep(Duration duration) => Task(() => Future.delayed(duration));

static Task<void> print(String s) => Task.delay(() => _consolePrint(s));
Expand Down
15 changes: 15 additions & 0 deletions test/task_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ void main() {
expect(result1, result2);
});

test('Task.fromEither', () async {
final taskLeft = await Task.value(1000)
.productR(Task.fromEither(left<String, int>('boom')))
.attempt()
.run();

final taskRight = await Task.fromEither(right<String, int>(42))
.replace(1000)
.attempt()
.run();

expect(taskLeft, left('boom'));
expect(taskRight, right(1000));
});

test("Task attempt", () async {
final Task<Either<Object, num>> t =
Task.delay(() => "notanumber").map(num.parse).attempt();
Expand Down