반응형
동기와 비동기 프로그래밍
node.js 문서를 보면
위는 비동기 버전이고
위는 동기 버전이다.
예제 asyncsync.js
var fs = require('fs');
console.log(1);
//sync
var data = fs.readFileSync('./data.txt', {encoding:'utf8'});
console.log(data);
console.log(2);
//async
var data1 = fs.readFile('./data.txt', {encoding:'utf8'}, function(err, data){
console.log(3);
console.log(data);
});
console.log(4);
실행 결과
1 2 3 4 가 순차적으로 실행되는 것이 아닌 1 2 4 3 으로 실행된다.. 즉 비동기 방식이 적용된 것이다.
출처 : 생활코딩 (https://opentutorials.org/course/2136)
'프로그래밍 > Node.js' 카테고리의 다른 글
Node.js 생활코딩 정리 - Express프레임워크 템플릿 엔진(Jade 또는 pug) 사용 방법 (0) | 2016.11.21 |
---|---|
Node.js 생활코딩 정리 – Express 프레임워크 동적방식과 정적방식 차이 (0) | 2016.11.21 |
Node.js 생활코딩 정리 – Express 프레임워크 정적 파일을 서비스하는 법 (0) | 2016.11.21 |
Node.js 생활코딩 정리 – Express 프레임워크 도입 (0) | 2016.11.21 |
Node.js 생활코딩 정리 – 모듈과 NPM(Node Package Manager)이란? (0) | 2016.11.21 |
Node.js - node.js에서 c/c++ 라이브러리 이용하기 (펌자료) (0) | 2015.01.17 |
Node.js - Node inspector를 통한 디버깅 방법 (0) | 2015.01.17 |
Node.js - socket.io api 간단 설명 (펌자료) (0) | 2015.01.10 |