Skip to content
Snippets Groups Projects
Commit 85e0374e authored by Kaushitha K S's avatar Kaushitha K S
Browse files

Merge branch 'node' into 'master'

Create file using fs module with async/await

See merge request !1
parents efa9c04c 4e631bf4
No related branches found
No related tags found
1 merge request!1Create file using fs module with async/await
index.js 0 → 100644
// index.js
function displayMessage() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Hello from the promise!");
}, 1000);
});
}
displayMessage()
.then(msg => console.log(msg))
.catch(err => console.error(err));
const fs = require('fs').promises;
async function createFile() {
try {
await fs.writeFile('output.txt', 'This file was created using fs and async/await!');
console.log('File created successfully!');
} catch (error) {
console.error('Error writing file:', error);
}
}
// Call both functions
async function main() {
const message = await displayMessage();
console.log(message);
await createFile();
}
main();
{
"name": "promise-fs-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment