Skip to content

Commit

Permalink
Merge pull request #2 from golemcloud/sync-fetch-support
Browse files Browse the repository at this point in the history
Add synchronous fetch support functions
  • Loading branch information
noise64 authored Jun 14, 2024
2 parents 9ff8723 + 5ba33b3 commit d9637db
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/eventloop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2024 Golem Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { Result } from "./result";

/**
* Workaround helper until async support arrives: runEventLoopUntilInterest keeps
* running the event loop until all pending async tasks and jobs (e.g. Fetch promises)
* are finished.
*/
export declare const runEventLoopUntilInterest: () => void;

/**
* Waits for a promise (by using runEventLoopUntilInterest) and returns
* its result synchronously. In case of rejection it rethrows the error.
* @param promise
*/
export function asyncToSync<T>(promise: Promise<T>): T {
let success = false;
let done = false;
let result: T;
let error: any;

promise
.then((r) => {
result = r;
success = true;
done = true;
})
.catch((e) => {
error = e;
done = true;
});

runEventLoopUntilInterest();

if (!done) {
throw new Error("asyncToSync: illegal state: not done");
}

if (!success) {
throw error;
}

return result;
}

/**
* Result returning variant of asyncToSync;
* @param promise
*/
export function asyncToSyncAsResult<T>(promise: Promise<T>): Result<T, any> {
return Result.tryCatch(() => asyncToSync(promise));
}
34 changes: 34 additions & 0 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 Golem Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { asyncToSync, asyncToSyncAsResult } from "./eventloop";
import type { Result } from "./result";

/**
* Synchronous wrapper of fetch
* @param input
* @param init
*/
function fetchSync(input: RequestInfo | URL, init?: RequestInit): Response {
return asyncToSync(fetch(input, init));
}

/**
* Result returning variant of fetchSync
* @param input
* @param init
*/
function fetchSyncAsResult(input: RequestInfo | URL, init?: RequestInit): Result<Response, any> {
return asyncToSyncAsResult(fetch(input, init));
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.

export * from "./bindgen/bindgen";
export * from "./eventloop";
export * from "./fetch";
export * from "./guard";
export * from "./result";
export * from "./transaction";

0 comments on commit d9637db

Please sign in to comment.