Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP effection retry blogpost #374

Draft
wants to merge 4 commits into
base: production
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add arguments for fetchWithBackoff
minkimcello committed Feb 13, 2024
commit f24ccbebf53254154d1711d2feb1e3c6358568dc
16 changes: 8 additions & 8 deletions legacy/src/blog/2024-02-19-retries-with-effection.md
Original file line number Diff line number Diff line change
@@ -19,17 +19,17 @@ Writing a simple fetch call using effection
```js
import { run, useAbortSignal, call } from 'effection';

function* fetchURL() {
function* fetchURL(url: URL | string, init?: RequestInit) {
const signal = yield* useAbortSignal();
const response = yield* call(fetch("https://foo.bar"), { signal });
const response = yield* call(fetch(url), { ...init, signal });

if (response.ok) {
return yield* call(() => response.json());
}
}

run(function* () {
const result = yield* fetchURL();
const result = yield* fetchURL(url);
console.log(result);
});
```
@@ -43,11 +43,11 @@ Let's add retry logic with exponential backoff
```js
import { run, useAbortSignal, call, sleep } from 'effection';

function* fetchWithBackoff() {
function* fetchWithBackoff(url: URL | string, init?: RequestInit) {
let attempt = -1;
while (true) {
const signal = yield* useAbortSignal();
const response = yield* call(fetch("https://foo.bar"), { signal });
const response = yield* call(fetch(url), { ...init, signal });

if (response.ok) {
return yield* call(() => response.json());
@@ -68,7 +68,7 @@ function* fetchWithBackoff() {
}

run(function* () {
const result = yield* fetchWithBackoff();
const result = yield* fetchWithBackoff("https://foo.bar");
console.log(result);
});
```
@@ -82,7 +82,7 @@ Now let's add a timeout using race
```js
import { run, useAbortSignal, call, sleep, race } from 'effection';

function* fetchWithBackoff() {
function* fetchWithBackoff(url: URL | string, init?: RequestInit) {
let attempt = -1;
while (true) {
const signal = yield* useAbortSignal();
@@ -104,7 +104,7 @@ function* fetchWithBackoff() {

run(function* () {
const result = yield* race([
fetchWithBackoff(),
fetchWithBackoff("https://foo.bar"),
sleep(60_000),
]);
console.log(result);