Skip to content

Commit

Permalink
add basic tla execution test
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Jul 6, 2023
1 parent b7d8009 commit d2cfc7c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// import() doesn't care about whether a module is an async module or not
const UserApi = import("./UserAPI.js");

export const CreateUserAction = async (name) => {
console.log("Creating user", name);
// These are normal awaits, because they are in an async function
const { createUser } = await UserApi;
return await createUser(name);
};

// You can place import() where you like
// Placing it at top-level will start loading and evaluating on
// module evaluation.
// see CreateUserAction above
// Here: Connecting to the DB starts when the application starts
// Placing it inside of an (async) function will start loading
// and evaluating when the function is called for the first time
// which basically makes it lazy-loaded.
// see AlternativeCreateUserAction below
// Here: Connecting to the DB starts when AlternativeCreateUserAction
// is called
export const AlternativeCreateUserAction = async (name) => {
const { createUser } = await import("./UserAPI.js");
return await createUser(name);
};

// Note: Using await import() at top-level doesn't make much sense
// except in rare cases. It will import modules sequentially.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Adapted from webpack
https://github.com/webpack/webpack/blob/6be4065ade1e252c1d8dcba4af0f43e32af1bdc1/examples/top-level-await/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { dbCall } from "./db-connection.js";

export const createUser = async (name) => {
const command = `CREATE USER ${name}`;
// This is a normal await, because it's in an async function
return await dbCall({ command });
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const connectToDB = async (url) => {
console.log("connecting to db", url);
await new Promise((r) => setTimeout(r, 1000));
};

// This is a top-level-await
await connectToDB("my-sql://example.com");

export const dbCall = async (data) => {
console.log("dbCall", data);
// This is a normal await, because it's in an async function
await new Promise((r) => setTimeout(r, 100));
return "fake data";
};

export const close = () => {
console.log("closes the DB connection");
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { CreateUserAction, AlternativeCreateUserAction } from "./Actions.js";

it("should handle top level await", async () => {
const res = await CreateUserAction("John");
expect(res).toBe("fake data");
});

it("should handle top level await (alternative)", async () => {
const res = await AlternativeCreateUserAction("John");
expect(res).toBe("fake data");
});

0 comments on commit d2cfc7c

Please sign in to comment.