-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c53594b
commit cd632cb
Showing
5 changed files
with
109 additions
and
0 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 @@ | ||
{ | ||
"Effect Gen Function": { | ||
"prefix": "egen", | ||
"body": ["Effect.gen(function* ($) {\n\t$0\n})"], | ||
"description": "Effect generator FUnction with $ input" | ||
}, | ||
"Gen Function": { | ||
"prefix": "gen", | ||
"body": ["function* ($) {}"], | ||
"description": "Generator FUnction with $ input" | ||
}, | ||
"Gen Yield * tmp": { | ||
"prefix": "yy", | ||
"body": ["yield* $($0)"], | ||
"description": "Yield generator calling $()" | ||
}, | ||
"Gen Yield *": { | ||
"prefix": "!", | ||
"body": ["yield* $($0)"], | ||
"description": "Yield generator calling $()" | ||
} | ||
} |
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
{ | ||
devShell = with pkgs; pkgs.mkShell { | ||
buildInputs = [ | ||
act | ||
nodejs_20 | ||
corepack | ||
]; | ||
|
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,51 @@ | ||
import * as Node from '@effect/platform-node/Runtime' | ||
import { Cause, Duration, Effect } from 'effect' | ||
import { TaggedClass } from 'effect/Data' | ||
|
||
// --- HELPERS --- | ||
|
||
const getMagicNumber = async () => { | ||
await sleep(200) | ||
|
||
return 42 | ||
} | ||
|
||
const throwSomeError = async () => { | ||
throw new Error('some error') | ||
} | ||
|
||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) | ||
|
||
class MyCustomError extends TaggedClass('MyCustomError')<{ readonly cause: unknown }> {} | ||
|
||
const someEffect = Effect.succeed(69).pipe(Effect.delay(Duration.seconds(0.5))) | ||
|
||
const someAsyncFnRunningAnEffect = async () => { | ||
const result = await Effect.runPromise(someEffect) | ||
|
||
return result | ||
} | ||
|
||
// --- MAIN --- | ||
|
||
const main = Effect.gen(function* ($) { | ||
// calling a async function/promise from Effect | ||
const result1 = yield* $(Effect.promise(() => getMagicNumber())) | ||
|
||
console.log('Got result 1:', result1) | ||
|
||
// calling a async function/promise from Effect (with error handling) | ||
const result2 = yield* $( | ||
Effect.tryPromise({ try: () => throwSomeError(), catch: (cause) => new MyCustomError({ cause }) }), | ||
Effect.catchTag('MyCustomError', (error) => Effect.succeed(`Got error: ${error.cause}`)), | ||
) | ||
|
||
console.log('Got result 2:', result2) | ||
|
||
// calling an Effect from an async function/promise | ||
const result3 = yield* $(Effect.promise(() => someAsyncFnRunningAnEffect())) | ||
|
||
console.log('Got result 3:', result3) | ||
}) | ||
|
||
Node.runMain(main.pipe(Effect.tapErrorCause((_) => Effect.log(Cause.pretty(_))))) |
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,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"noEmit": true, | ||
"module": "Node16", | ||
"moduleResolution": "Node16", | ||
"strict": true, | ||
"plugins": [{ "name": "@effect/language-service" }], | ||
}, | ||
"include": ["src/**/*.ts"] | ||
} |