프로그래밍/Node.js

Node.js 생활코딩 정리 – 동기와 비동기 프로그래밍 이란?

가카리 2016. 11. 21. 20:22
반응형

동기와 비동기 프로그래밍

 

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)

반응형