Skip to content

Commit b5af693

Browse files
committed
feat: working on chapter 11
1 parent e1472b5 commit b5af693

File tree

15 files changed

+282
-0
lines changed

15 files changed

+282
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 01-local-initialization-check
2+
3+
This sample showcases some simple solutions to the problem of asynchronously
4+
initialized modules.
5+
6+
## Run
7+
8+
There are 4 different examples in this folder.
9+
10+
### 1. Wrong initialization (not calling `.connect()`)
11+
12+
Execute it with:
13+
14+
```
15+
node wrong-init-no-connect.js
16+
```
17+
18+
### 2. Wrong initialization (calling `.connect()` but not waiting for it)
19+
20+
Execute it with:
21+
22+
```
23+
node wrong-init-no-await.js
24+
```
25+
26+
### 3. Correct initialization (using a local initialization check)
27+
28+
Execute it with:
29+
30+
```
31+
node localInitializationCheck.js
32+
```
33+
34+
### 4. Correct initialization (using the delayed startup pattern)
35+
36+
Execute it with:
37+
38+
```
39+
node delayedStartup.js
40+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { setTimeout } from 'node:timers/promises'
2+
3+
class Database {
4+
connected = false
5+
6+
async connect() {
7+
if (!this.connected) {
8+
// simulate the delay of the connection
9+
await setTimeout(500)
10+
this.connected = true
11+
}
12+
}
13+
14+
async query(queryString) {
15+
if (!this.connected) {
16+
throw new Error('Not connected yet')
17+
}
18+
// simulate the delay of the query execution
19+
await setTimeout(100)
20+
console.log(`Query executed: ${queryString}`)
21+
}
22+
}
23+
24+
export const db = new Database()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { db } from './db.js'
2+
3+
async function getConnectedDb() {
4+
await db.connect()
5+
return db
6+
}
7+
8+
async function getUsers(db) {
9+
await db.query('SELECT * FROM users')
10+
}
11+
12+
const connectedDb = await getConnectedDb()
13+
await getUsers(connectedDb)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { db } from './db.js'
2+
3+
async function getUsers() {
4+
if (!db.connected) {
5+
await db.connect()
6+
}
7+
8+
await db.query('SELECT * FROM users')
9+
}
10+
11+
await getUsers()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "01-local-initialization-check",
3+
"version": "1.0.0",
4+
"description": "This sample showcases some simple solutions to the problem of asynchronously initialized modules.",
5+
"type": "module",
6+
"scripts": {},
7+
"engines": {
8+
"node": ">=23"
9+
},
10+
"engineStrict": true,
11+
"keywords": [],
12+
"author": "Luciano Mammino and Mario Casciaro",
13+
"license": "MIT",
14+
"dependencies": {}
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { db } from './db.js'
2+
3+
db.connect()
4+
const users = await db.query('SELECT * FROM users')
5+
console.log(users)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { db } from './db.js'
2+
3+
const users = await db.query('SELECT * FROM users')
4+
console.log(users)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 02-async-init-queue
2+
3+
This sample showcases how to use pre-initialization queues with an
4+
asynchronously initialized component.
5+
6+
## Run
7+
8+
To run the example launch:
9+
10+
```
11+
node index.js
12+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { setTimeout } from 'node:timers/promises'
2+
3+
class Database {
4+
connected = false
5+
commandsQueue = []
6+
7+
async connect() {
8+
// simulate the delay of the connection
9+
await setTimeout(500)
10+
this.connected = true
11+
while (this.commandsQueue.length > 0) {
12+
const command = this.commandsQueue.shift()
13+
command()
14+
}
15+
}
16+
17+
async query(queryString) {
18+
if (!this.connected) {
19+
console.log(`Request queued: ${queryString}`)
20+
21+
return new Promise((resolve, reject) => {
22+
const command = () => {
23+
this.query(queryString).then(resolve, reject)
24+
}
25+
this.commandsQueue.push(command)
26+
})
27+
}
28+
29+
// simulate the delay of the query execution
30+
await setTimeout(100)
31+
console.log(`Query executed: ${queryString}`)
32+
}
33+
}
34+
35+
export const db = new Database()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { setTimeout } from 'node:timers/promises'
2+
import { db } from './db.js'
3+
4+
db.connect()
5+
6+
async function updateLastAccess() {
7+
await db.query(`INSERT (${Date.now()}) INTO "LastAccesses"`)
8+
}
9+
10+
updateLastAccess()
11+
await setTimeout(600)
12+
updateLastAccess()

0 commit comments

Comments
 (0)