Skip to content

Commit

Permalink
simplify Do by removing unwrapping function
Browse files Browse the repository at this point in the history
  • Loading branch information
cevr committed Apr 28, 2024
1 parent 34ccf39 commit f62b683
Show file tree
Hide file tree
Showing 14 changed files with 2,851 additions and 2,868 deletions.
27 changes: 27 additions & 0 deletions .changeset/nine-terms-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
"ftld": major
---

Simplify Do, remove promise support.

Do no longer needs the unwrapper function.

```
// before
const x = Do(function*($) {
const a = yield* $(Result.Ok(1))
const b = yield* $(Option.Some(2))
return a + b;
});
// after
const x = Do(function*() {
const a = yield* Result.Ok(1);
const b = yield* Option.Some(2);
return a + b;
});
```

A side effect of this is that unwrapping promises is no longer supported. This is a breaking change, but it is for the better.

Promises are not a good fit for the Do notation, and it is better to use async/await instead. Promises also have no way of tracking the Error type, which is a big limitation.
63 changes: 15 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -794,23 +794,19 @@ function doSomething(): AsyncTask<
SomeError | OtherError | UnwrapNoneError, // <-- notice how the Option type has an error type
number
> {
return Do(function* ($) {
const a: number = yield* $(
Result.from(
() => 1,
() => new SomeError()
)
return Do(function* () {
const a: number = yield* Result.from(
() => 1,
() => new SomeError()
);

// async!
const b: number = yield* $(
Task.from(
async () => 2,
() => new OtherError()
)
const b: number = yield* Task.from(
async () => 2,
() => new OtherError()
);

const c: number = yield* $(Option.from(3 as number | null));
const c: number = yield* Option.from(3 as number | null);

return a + b + c;
});
Expand All @@ -822,50 +818,21 @@ function doSomething(): SyncTask<
number
> {
return Do(function* ($) {
const a: number = yield* $(
Result.from(
() => 1,
() => new SomeError()
)
const a: number = yield* Result.from(
() => 1,
() => new SomeError()
);

const b: number = yield* $(
Result.from(
() => 2,
() => new OtherError()
)
const b: number = yield* Result.from(
() => 2,
() => new OtherError()
);

const c: number = yield* $(Option.from(3 as number | null));
const c: number = yield* Option.from(3 as number | null);

return a + b + c;
});
}

// you can also use Do with Promises
// and quickly declare the error type

async function calculateNum(): Promise<number>;

function doSomething(): AsyncTask<SomeError | AnotherError, number> {
return Do(function* ($) {
const a = yield* $(
calculateNum(),
(e) => new SomeError() // <-- notice how the error can be mapped over
//^-- the error type is inferred initially as unknown which captures the initial error in a value `.error`
);

// can quickly override the error type for any type
const b = yield* $(
Task.from(
() => 1,
() => new OtherError()
),
(e: OtherError) => new AnotherError()
// ^-- the error type is inferred
);
});
}
```

## Recipes
Expand Down
8 changes: 4 additions & 4 deletions lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
"test:coverage": "vitest run --coverage && open coverage/index.html"
},
"devDependencies": {
"@vitest/coverage-v8": "^1.5.0",
"bunchee": "^5.1.2",
"@vitest/coverage-v8": "^1.5.2",
"bunchee": "^5.1.3",
"concurrently": "^8.2.2",
"typescript": "^5.4.5",
"undici": "^6.13.0",
"vitest": "^1.5.0"
"undici": "^6.14.1",
"vitest": "^1.5.2"
},
"files": [
"dist"
Expand Down
Loading

0 comments on commit f62b683

Please sign in to comment.