Skip to content

Commit

Permalink
Add arguments for fetchWithBackoff
Browse files Browse the repository at this point in the history
  • Loading branch information
minkimcello committed Feb 13, 2024
1 parent 8935235 commit f24ccbe
Showing 1 changed file with 8 additions and 8 deletions.
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
Expand Up @@ -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);
});
```
Expand All @@ -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());
Expand All @@ -68,7 +68,7 @@ function* fetchWithBackoff() {
}

run(function* () {
const result = yield* fetchWithBackoff();
const result = yield* fetchWithBackoff("https://foo.bar");
console.log(result);
});
```
Expand All @@ -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();
Expand All @@ -104,7 +104,7 @@ function* fetchWithBackoff() {

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

0 comments on commit f24ccbe

Please sign in to comment.