Last active
October 3, 2025 21:47
-
-
Save myaosato/a6f97b9b69255a73d3ea57e6b0e4d8c6 to your computer and use it in GitHub Desktop.
ResponseHandler.ts
This file contains hidden or 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
| export class ResponseHandler { | |
| private promise: Promise<Response>; | |
| private constructor(promise: Promise<Response>) { | |
| this.promise = promise; | |
| }; | |
| public receive(status: number, handler: (response: Response) => void) { | |
| const next = this.promise.then((response: Response) => { | |
| if (status === response.status) { | |
| handler(response); | |
| } | |
| return response; | |
| }); | |
| return new ResponseHandler(next); | |
| }; | |
| public failure(handler: (reason: unknown) => void) { | |
| const next = this.promise.catch((reason) => { | |
| handler(reason); | |
| return this.promise; | |
| }); | |
| return new ResponseHandler(next); | |
| } | |
| public bottom(handler: () => void) { | |
| const next = this.promise.finally(handler); | |
| return new ResponseHandler(next); | |
| } | |
| public static call(input: URL | RequestInfo, init?: RequestInit) { | |
| return new ResponseHandler(fetch(input, init)); | |
| } | |
| }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment