-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7-gpt.js
28 lines (23 loc) · 824 Bytes
/
7-gpt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'use strict';
// Chat GPT version 4
// Prompt: Optimize without moving console.log inside try block and without let
// (use just const)
// Reply: Understood. Here's an optimized version of the code without
// using let and keeping console.log outside the try-catch block:
const fs = require('node:fs').promises;
(async () => {
const config = await (async () => {
try {
const data = await fs.readFile('config.json', 'utf8');
return JSON.parse(data);
} catch {
return { port: 80 };
}
})();
console.log({ config });
})();
// Chat GPT comments:
// This version wraps the try-catch block inside an immediately invoked
// async function expression. This allows you to use const for the config
// variable and maintain the scope of the console.log statement outside
// the try-catch block.