쓰기와 마찬가지로 동기 비동기가 있다


비동기 파일 쓰기 처리
 /**
     * Asynchronously writes data to a file, replacing the file if it already exists.
     * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
     * URL support is _experimental_.
     * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
     * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.
     */
    export function writeFile(path: PathLike | number, data: any, callback: (err: NodeJS.ErrnoException) => void): void;




동기식 파일 쓰기 처리
/**
     * Synchronously writes data to a file, replacing the file if it already exists.
     * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
     * URL support is _experimental_.
     * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
     * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.
     * @param options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag.
     * If `encoding` is not supplied, the default of `'utf8'` is used.
     * If `mode` is not supplied, the default of `0o666` is used.
     * If `mode` is a string, it is parsed as an octal integer.
     * If `flag` is not supplied, the default of `'w'` is used.
     */
    export function writeFileSync(path: PathLike | number, data: any, options?: { encoding?: string | null; mode?: number | string; flag?: string; } | string | null): void;







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var fs = require('fs');
 
 
//동기 파일 쓰기
//fs.writeFileSync
 
//비동기 파일 쓰기, 
fs.writeFile('./output.txt''hello.'function (err) {
    if (err) {
        console.log('에러발생');
        console.dir(err);
        return;
    }
    console.log('파일쓰기완료'); 
   }
);
console.log('파일 쓰기 명령');
   




결과 : 


비동기 기준임으로 파일 스기 명령이 먼저 찍힌 것을 볼 수 있다





써진 내용




반응형

+ Recent posts