-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7d8009
commit d2cfc7c
Showing
5 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
crates/turbopack-tests/tests/execution/turbopack/basic/top-level-await/input/Actions.js
This file contains 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
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. |
2 changes: 2 additions & 0 deletions
2
...turbopack-tests/tests/execution/turbopack/basic/top-level-await/input/README.md
This file contains 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
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 |
7 changes: 7 additions & 0 deletions
7
crates/turbopack-tests/tests/execution/turbopack/basic/top-level-await/input/UserAPI.js
This file contains 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
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 }); | ||
}; |
18 changes: 18 additions & 0 deletions
18
...es/turbopack-tests/tests/execution/turbopack/basic/top-level-await/input/db-connection.js
This file contains 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
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"); | ||
}; |
11 changes: 11 additions & 0 deletions
11
crates/turbopack-tests/tests/execution/turbopack/basic/top-level-await/input/index.js
This file contains 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
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"); | ||
}); |