Asynchronous form always completion callback as its last argument . Pass completion callback parameters depend on the specific method, but the first parameter is always reserved for abnormalities . If the operation is completed successfully, the first argument will be null
or undefined
.
1. Read the file operation (readFile)
For example (file read operation) will first synchronization after executing [try to use asynchronous]
the require FS = const ( 'FS' ) fs.readFile ( './30_readfile.js', 'UTF8', (ERR, Data) => { IF (ERR) { the throw ERR } the else { // The output is a Buffer (because there is no toString or parameters define the encoding format) the console.log (Data); // the console.log (data.toString ()); } }) // synchronization const data = fs.readFileSync ( './ 29_event_remove.js', 'UTF8' ); the console.log (Data);
2. Write file ( writeFile )
//输出done const fs = require('fs') fs.writeFile('./text','this is text','utf8',err=>{ if(err) throw err; console.log('done') }) //输出done 使用了Buffer const fs = require('fs') const content = Buffer.from('this is text') fs.writeFile('./text',content,err=>{ if(err) throw err; console.log('done') })
3. view the file information (stats)
const fs =require('fs') fs.stat('./32_stat.js',(err,stats)=>{ if(err){ throw err }else{ console.log(stats.isFile()); console.log(stats.isDirectory()); console.log(stats); } })
operation result:
4. Rename (the rename)
const fs =require('fs') fs.rename('./text','text.txt',err=>{ if(err) throw err console.log('done!'); })
5.unlink
const fs = require('fs')
fs.unlink('./text.txt',err=>{});
6. Read folder (readdir)
const fs = require('fs') fs.readdir('./',(err,files)=>{ if(err) throw err console.log(files); })
operation result
7. Create a folder (mkdir)
const fs =require('fs')
fs.mkdir('test',err=>{ })
8. Remove a folder (rmdir)
const fs = require('fs')
fs.rmdir('./test',err=>{});
9. Monitoring (Watch) [useful, as the local build very convenient]
const fs = require('fs') fs.watch('./',{ recursive:true },(eventType,filename)=>{ console.log(eventType,filename); });
10.readstream
// Stream directional flow (data) by Stream There are two conditions, one is a data direction const the require FS = ( 'FS' ) const RS = fs.createReadStream ( './ 39_readstream.js' ); // to read a little bit, similar to loading from top to bottom Suman rs.pipe (process.stdout);
11.writestream
const fs =require('fs') const ws = fs.createWriteStream('./text.txt') const tid = setInterval(()=>{ const num = parseInt(Math.random()*10) console.log(num); if(num<8){ ws.write(num + '') }else{ clearInterval(tid) ws.end() } },200) ws.on('finish',()=>{ console.log('done!'); })
operation result
12. callback hell resolved
method one
const fs =require('fs') const promisify = require('util').promisify; const read = promisify(fs.readFile) read('./41_promisify.js').then(data=>{ console.log(data.toString()); }).catch(ex=>{ console.log(ex); })
operation result
Method Two
const fs =require('fs') const promisify = require('util').promisify; const read = promisify(fs.readFile) // read('./41_promisify.js').then(data=>{ // console.log(data.toString()); // }).catch(ex=>{ // console.log(ex); // }) async function test(){ try{ const content= await read('./41_promisify.js') console.log(content.toString()); }catch(ex){ console.log(ex); } } test();
Like the above operating results