-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs_asyncw-await1.js
40 lines (37 loc) · 1.13 KB
/
fs_asyncw-await1.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
29
30
31
32
33
34
35
36
37
38
39
40
const {readFile,writeFile} = require("fs");
function getText (path){
return new Promise((resolve, reject)=>{
readFile(path,"utf-8",(err,data)=>{
if(err){
reject(err);
return;
}
resolve(data);
})
})
}
function writeText(path,text){
return new Promise((resolve, reject)=>{
writeFile(path,text,(err,data)=>{
if(err){
reject(err);
return;
}
resolve(data); //As it returns undefined, it is not required
})
})
}
const fileManager= async ()=>{ //async function fileManager(){} likheo kora jeto
try
{
const first= await getText("testFiles/hello.txt");
const second= await getText("testFiles/dele.txt");
await writeText("./swee2.txt",`Sweetu is awesome:${first} and ${second}`); //writeFile er resolve part ta undefined return kore, tai otake variable er moddhe store korlam na.
console.log(first,second);
}
catch(error){
console.log(error);
}
}
fileManager();
//Remarks: This makes our code a lot cleaner but we need to write some code for writeFile,lets see.