SlideShare uma empresa Scribd logo
1 de 10
Promise에 대한 고찰 
Pin the Cloud 
유홍근
Promise 패턴이란 
new Promise(function(fulfill, reject) { 
mssql.query(“SELECT * …”, { 
// async callback 
success: function(results) { 
// tell Promise obj that async task is done 
fulfill(results); 
} 
}); 
}).done(function(results){ 
// do some post work after async task is done 
results………. 
}); 
참고 페이지 : https://www.promisejs.org/
Promise의 장점 
• Inner block을 없애준다. 
• Async 작업을 synchronous하게 만들어준다. 
(개발의 흐름이랑 실제 코딩이랑 맞춰준다.) 
• 스파게티 코드 없애줌. (Dependency 줄여줌) 
• 현재 크롬 브라우저 경우는 언어 차원에서 
지원해주는거 같음. (라이브러리 안써도 
잘됨)
Compare with Async Chainer 
asyncChariner.async([ 
function() { 
asyncChainer.executeAsync(result); 
}, function(result) { 
….. 
asyncChainer.executeAsync(val); 
}, function(val) { 
// end of async chain 
} 
]); 
new Promise(function(fulfill, reject) { 
mssql.query(“SELECT * …”, { 
// async callback 
success: function(results) { 
fulfill(results); 
} 
}); 
}).then(function(results){ 
results………. 
return val; 
}).then(function(val){ 
val….. 
});
Promise 핵심 원리 
• Spring Framework 에서 나오는 Ioc/DI와 
상당히 비슷한 개념으로 이해하면 쉬움. 
• Inversion of callback (내가 만든 용어임ㅋㅋ) 
• 혹은 Callback of callback (이것뚜ㅋㅋㅋ) 
caller() 
callee() 
callback callback 
callback 
of 
callback 
caller() 
callee()
Source code 
// inversion of callback 
function callee(callback) { 
// callback func has a callback 
callback(function(param){ 
console.log(param); 
}); 
} 
function caller() { 
// caller calls callee 
callee(function(func){ 
// the param of the callback is 
a function 
func(123); 
}); 
} 
// also prints 123 
// typical callback function 
function callee(callback) { 
callback(123); 
} 
function caller() { 
// caller calls callee 
// in our case, 
mssql.query() 
callee(function(result){ 
// in callback func 
console.log(result); 
}); 
} 
// prints 123
핵심 원리 (Cont.) 
• 콜백 함수의 파라미터 값이 function이고, 
콜백 호출시 파라미터로 받은 함수를 
호출하게 되면 callback of callback이 된다. 
• (중요)이를 통해 콜백 함수(callee)가 호출된 
시점을 다시 콜백 함수를 호출 시킨 
함수(caller)에 알려줄 수가 있다!! 
• 그렇게 되면 다음 순서의 async task 호출 
시점을 알 수 있게 된다. (문제 해결)
그림으로 Async Chainer 비교 
AsyncChainer 
객체 
Callback 
(End of async task) 
Next Registered 
Async Task 
나 
끝났음~ 
그래? 
알겠음~ 
다음 차례 
시작하셈~ 
ㅇㅋ 
AsyncChainer 객체한테 알려주면 그 객체가 직접 다음 작업을 
호출한다.
그림으로 Async Chainer 비교 
Callback 
(End of async task) 
Next Registered 
Async Task 
callback 
Callback of Callback 
(implement in Promise) 
callback of callback
개인적인 의견 
• 결국엔 객체를 통해서 다음 작업을 호출 
시점을 정하느냐 함수를 통해서 하냐 
차이인듯. 
• Functional Language의 특성을 백분 활용한 
기법인거 같다는 생각. 
• 객체지향에서는 가능하지 않는(어쩌면 
람다를 통해서 가능한데 힘든) function 
language의 위력을 깨닫는 공부였음.

Mais conteúdo relacionado

Mais procurados

골때리는 자바스크립트 발표자료
골때리는 자바스크립트 발표자료골때리는 자바스크립트 발표자료
골때리는 자바스크립트 발표자료욱진 양
 
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)Circulus
 
Angular2 가기전 Type script소개
 Angular2 가기전 Type script소개 Angular2 가기전 Type script소개
Angular2 가기전 Type script소개Dong Jun Kwon
 
ECMAScript 6의 새로운 것들!
ECMAScript 6의 새로운 것들!ECMAScript 6의 새로운 것들!
ECMAScript 6의 새로운 것들!WooYoung Cho
 
[143] Modern C++ 무조건 써야 해?
[143] Modern C++ 무조건 써야 해?[143] Modern C++ 무조건 써야 해?
[143] Modern C++ 무조건 써야 해?NAVER D2
 
Learning Node Book, Chapter 5
Learning Node Book, Chapter 5Learning Node Book, Chapter 5
Learning Node Book, Chapter 5Ji Hun Kim
 
[D2 COMMUNITY] ECMAScript 2015 S67 seminar - 1. primitive
[D2 COMMUNITY] ECMAScript 2015 S67 seminar - 1. primitive[D2 COMMUNITY] ECMAScript 2015 S67 seminar - 1. primitive
[D2 COMMUNITY] ECMAScript 2015 S67 seminar - 1. primitiveNAVER D2
 
Javascript - Function
Javascript - FunctionJavascript - Function
Javascript - Functionwonmin lee
 
Es2015 Simple Overview
Es2015 Simple OverviewEs2015 Simple Overview
Es2015 Simple OverviewKim Hunmin
 
7급 공무원도 쉽게 따라하는 쉘 스크립트
7급 공무원도 쉽게 따라하는 쉘 스크립트7급 공무원도 쉽게 따라하는 쉘 스크립트
7급 공무원도 쉽게 따라하는 쉘 스크립트Young-Ho Cha
 
Hacosa js study 2주차
Hacosa js study 2주차Hacosa js study 2주차
Hacosa js study 2주차Seong Bong Ji
 
Javascript 조금 더 잘 알기
Javascript 조금 더 잘 알기Javascript 조금 더 잘 알기
Javascript 조금 더 잘 알기jongho jeong
 
Promise in javascript
Promise in javascriptPromise in javascript
Promise in javascriptnamwook lim
 
[1B4]안드로이드 동시성_프로그래밍
[1B4]안드로이드 동시성_프로그래밍[1B4]안드로이드 동시성_프로그래밍
[1B4]안드로이드 동시성_프로그래밍NAVER D2
 
Angular2 router&http
Angular2 router&httpAngular2 router&http
Angular2 router&httpDong Jun Kwon
 
Smc–state machinecompiler
Smc–state machinecompilerSmc–state machinecompiler
Smc–state machinecompilerDong Hyeun Lee
 

Mais procurados (20)

골때리는 자바스크립트 발표자료
골때리는 자바스크립트 발표자료골때리는 자바스크립트 발표자료
골때리는 자바스크립트 발표자료
 
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
 
Clojure Chapter.6
Clojure Chapter.6Clojure Chapter.6
Clojure Chapter.6
 
Angular2 가기전 Type script소개
 Angular2 가기전 Type script소개 Angular2 가기전 Type script소개
Angular2 가기전 Type script소개
 
ECMAScript 6의 새로운 것들!
ECMAScript 6의 새로운 것들!ECMAScript 6의 새로운 것들!
ECMAScript 6의 새로운 것들!
 
[143] Modern C++ 무조건 써야 해?
[143] Modern C++ 무조건 써야 해?[143] Modern C++ 무조건 써야 해?
[143] Modern C++ 무조건 써야 해?
 
Learning Node Book, Chapter 5
Learning Node Book, Chapter 5Learning Node Book, Chapter 5
Learning Node Book, Chapter 5
 
[D2 COMMUNITY] ECMAScript 2015 S67 seminar - 1. primitive
[D2 COMMUNITY] ECMAScript 2015 S67 seminar - 1. primitive[D2 COMMUNITY] ECMAScript 2015 S67 seminar - 1. primitive
[D2 COMMUNITY] ECMAScript 2015 S67 seminar - 1. primitive
 
Javascript - Function
Javascript - FunctionJavascript - Function
Javascript - Function
 
Enum
EnumEnum
Enum
 
Es2015 Simple Overview
Es2015 Simple OverviewEs2015 Simple Overview
Es2015 Simple Overview
 
7급 공무원도 쉽게 따라하는 쉘 스크립트
7급 공무원도 쉽게 따라하는 쉘 스크립트7급 공무원도 쉽게 따라하는 쉘 스크립트
7급 공무원도 쉽게 따라하는 쉘 스크립트
 
Hacosa js study 2주차
Hacosa js study 2주차Hacosa js study 2주차
Hacosa js study 2주차
 
shell and process
shell and processshell and process
shell and process
 
Javascript 조금 더 잘 알기
Javascript 조금 더 잘 알기Javascript 조금 더 잘 알기
Javascript 조금 더 잘 알기
 
Promise in javascript
Promise in javascriptPromise in javascript
Promise in javascript
 
[1B4]안드로이드 동시성_프로그래밍
[1B4]안드로이드 동시성_프로그래밍[1B4]안드로이드 동시성_프로그래밍
[1B4]안드로이드 동시성_프로그래밍
 
Angular2 router&http
Angular2 router&httpAngular2 router&http
Angular2 router&http
 
Smc–state machinecompiler
Smc–state machinecompilerSmc–state machinecompiler
Smc–state machinecompiler
 
What is the meteor?
What is the meteor?What is the meteor?
What is the meteor?
 

Destaque

Cascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the StreamsCascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the Streamsmattpodwysocki
 
Functional Reactive Programming With RxSwift
Functional Reactive Programming With RxSwiftFunctional Reactive Programming With RxSwift
Functional Reactive Programming With RxSwift선협 이
 
Functional Reactive Programming with RxJS
Functional Reactive Programming with RxJSFunctional Reactive Programming with RxJS
Functional Reactive Programming with RxJSstefanmayer13
 
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015NAVER / MusicPlatform
 
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기Jong Wook Kim
 
RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015Ben Lesh
 

Destaque (7)

Encuesta de Fortuño
Encuesta de FortuñoEncuesta de Fortuño
Encuesta de Fortuño
 
Cascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the StreamsCascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the Streams
 
Functional Reactive Programming With RxSwift
Functional Reactive Programming With RxSwiftFunctional Reactive Programming With RxSwift
Functional Reactive Programming With RxSwift
 
Functional Reactive Programming with RxJS
Functional Reactive Programming with RxJSFunctional Reactive Programming with RxJS
Functional Reactive Programming with RxJS
 
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
 
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
NDC14 - Rx와 Functional Reactive Programming으로 고성능 서버 만들기
 
RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015
 

Semelhante a Promise 패턴 공부

EcmaScript6(2015) Overview
EcmaScript6(2015) OverviewEcmaScript6(2015) Overview
EcmaScript6(2015) Overviewyongwoo Jeon
 
JavaScript Debugging (수업자료)
JavaScript Debugging (수업자료)JavaScript Debugging (수업자료)
JavaScript Debugging (수업자료)지수 윤
 
[Swift] Command
[Swift] Command[Swift] Command
[Swift] CommandBill Kim
 
.NET에서 비동기 프로그래밍 배우기
.NET에서 비동기 프로그래밍 배우기.NET에서 비동기 프로그래밍 배우기
.NET에서 비동기 프로그래밍 배우기Seong Won Mun
 
12장 상속 (고급)
12장 상속 (고급)12장 상속 (고급)
12장 상속 (고급)유석 남
 
스위프트 성능 이해하기
스위프트 성능 이해하기스위프트 성능 이해하기
스위프트 성능 이해하기Yongha Yoo
 
Hoons 닷넷 정기세미나
Hoons 닷넷 정기세미나Hoons 닷넷 정기세미나
Hoons 닷넷 정기세미나병걸 윤
 
[오픈소스컨설팅] 스카우터 사용자 가이드 2020
[오픈소스컨설팅] 스카우터 사용자 가이드 2020[오픈소스컨설팅] 스카우터 사용자 가이드 2020
[오픈소스컨설팅] 스카우터 사용자 가이드 2020Ji-Woong Choi
 
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍Young-Beom Rhee
 
Clojure/Chapter3
Clojure/Chapter3Clojure/Chapter3
Clojure/Chapter3destinycs
 
자바스크립트 함수
자바스크립트 함수자바스크립트 함수
자바스크립트 함수유진 변
 
Javascript 함수(function) 개념, 호출패턴, this, prototype, scope
Javascript 함수(function) 개념, 호출패턴, this, prototype, scopeJavascript 함수(function) 개념, 호출패턴, this, prototype, scope
Javascript 함수(function) 개념, 호출패턴, this, prototype, scopeYoung-Beom Rhee
 
Start IoT with JavaScript - 6.함수
Start IoT with JavaScript - 6.함수Start IoT with JavaScript - 6.함수
Start IoT with JavaScript - 6.함수Park Jonggun
 
[252] 증분 처리 플랫폼 cana 개발기
[252] 증분 처리 플랫폼 cana 개발기[252] 증분 처리 플랫폼 cana 개발기
[252] 증분 처리 플랫폼 cana 개발기NAVER D2
 
C Language I
C Language IC Language I
C Language ISuho Kwon
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide웅식 전
 

Semelhante a Promise 패턴 공부 (20)

EcmaScript6(2015) Overview
EcmaScript6(2015) OverviewEcmaScript6(2015) Overview
EcmaScript6(2015) Overview
 
JavaScript Debugging (수업자료)
JavaScript Debugging (수업자료)JavaScript Debugging (수업자료)
JavaScript Debugging (수업자료)
 
[Swift] Command
[Swift] Command[Swift] Command
[Swift] Command
 
.NET에서 비동기 프로그래밍 배우기
.NET에서 비동기 프로그래밍 배우기.NET에서 비동기 프로그래밍 배우기
.NET에서 비동기 프로그래밍 배우기
 
12장 상속 (고급)
12장 상속 (고급)12장 상속 (고급)
12장 상속 (고급)
 
javascript02
javascript02javascript02
javascript02
 
스위프트 성능 이해하기
스위프트 성능 이해하기스위프트 성능 이해하기
스위프트 성능 이해하기
 
함수적 사고 2장
함수적 사고 2장함수적 사고 2장
함수적 사고 2장
 
Hoons 닷넷 정기세미나
Hoons 닷넷 정기세미나Hoons 닷넷 정기세미나
Hoons 닷넷 정기세미나
 
[오픈소스컨설팅] 스카우터 사용자 가이드 2020
[오픈소스컨설팅] 스카우터 사용자 가이드 2020[오픈소스컨설팅] 스카우터 사용자 가이드 2020
[오픈소스컨설팅] 스카우터 사용자 가이드 2020
 
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
 
Clojure/Chapter3
Clojure/Chapter3Clojure/Chapter3
Clojure/Chapter3
 
ES6 for Node.js Study 2주차
ES6 for Node.js Study 2주차ES6 for Node.js Study 2주차
ES6 for Node.js Study 2주차
 
자바스크립트 함수
자바스크립트 함수자바스크립트 함수
자바스크립트 함수
 
Javascript 함수(function) 개념, 호출패턴, this, prototype, scope
Javascript 함수(function) 개념, 호출패턴, this, prototype, scopeJavascript 함수(function) 개념, 호출패턴, this, prototype, scope
Javascript 함수(function) 개념, 호출패턴, this, prototype, scope
 
Start IoT with JavaScript - 6.함수
Start IoT with JavaScript - 6.함수Start IoT with JavaScript - 6.함수
Start IoT with JavaScript - 6.함수
 
[252] 증분 처리 플랫폼 cana 개발기
[252] 증분 처리 플랫폼 cana 개발기[252] 증분 처리 플랫폼 cana 개발기
[252] 증분 처리 플랫폼 cana 개발기
 
Javascript
JavascriptJavascript
Javascript
 
C Language I
C Language IC Language I
C Language I
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide
 

Promise 패턴 공부

  • 1. Promise에 대한 고찰 Pin the Cloud 유홍근
  • 2. Promise 패턴이란 new Promise(function(fulfill, reject) { mssql.query(“SELECT * …”, { // async callback success: function(results) { // tell Promise obj that async task is done fulfill(results); } }); }).done(function(results){ // do some post work after async task is done results………. }); 참고 페이지 : https://www.promisejs.org/
  • 3. Promise의 장점 • Inner block을 없애준다. • Async 작업을 synchronous하게 만들어준다. (개발의 흐름이랑 실제 코딩이랑 맞춰준다.) • 스파게티 코드 없애줌. (Dependency 줄여줌) • 현재 크롬 브라우저 경우는 언어 차원에서 지원해주는거 같음. (라이브러리 안써도 잘됨)
  • 4. Compare with Async Chainer asyncChariner.async([ function() { asyncChainer.executeAsync(result); }, function(result) { ….. asyncChainer.executeAsync(val); }, function(val) { // end of async chain } ]); new Promise(function(fulfill, reject) { mssql.query(“SELECT * …”, { // async callback success: function(results) { fulfill(results); } }); }).then(function(results){ results………. return val; }).then(function(val){ val….. });
  • 5. Promise 핵심 원리 • Spring Framework 에서 나오는 Ioc/DI와 상당히 비슷한 개념으로 이해하면 쉬움. • Inversion of callback (내가 만든 용어임ㅋㅋ) • 혹은 Callback of callback (이것뚜ㅋㅋㅋ) caller() callee() callback callback callback of callback caller() callee()
  • 6. Source code // inversion of callback function callee(callback) { // callback func has a callback callback(function(param){ console.log(param); }); } function caller() { // caller calls callee callee(function(func){ // the param of the callback is a function func(123); }); } // also prints 123 // typical callback function function callee(callback) { callback(123); } function caller() { // caller calls callee // in our case, mssql.query() callee(function(result){ // in callback func console.log(result); }); } // prints 123
  • 7. 핵심 원리 (Cont.) • 콜백 함수의 파라미터 값이 function이고, 콜백 호출시 파라미터로 받은 함수를 호출하게 되면 callback of callback이 된다. • (중요)이를 통해 콜백 함수(callee)가 호출된 시점을 다시 콜백 함수를 호출 시킨 함수(caller)에 알려줄 수가 있다!! • 그렇게 되면 다음 순서의 async task 호출 시점을 알 수 있게 된다. (문제 해결)
  • 8. 그림으로 Async Chainer 비교 AsyncChainer 객체 Callback (End of async task) Next Registered Async Task 나 끝났음~ 그래? 알겠음~ 다음 차례 시작하셈~ ㅇㅋ AsyncChainer 객체한테 알려주면 그 객체가 직접 다음 작업을 호출한다.
  • 9. 그림으로 Async Chainer 비교 Callback (End of async task) Next Registered Async Task callback Callback of Callback (implement in Promise) callback of callback
  • 10. 개인적인 의견 • 결국엔 객체를 통해서 다음 작업을 호출 시점을 정하느냐 함수를 통해서 하냐 차이인듯. • Functional Language의 특성을 백분 활용한 기법인거 같다는 생각. • 객체지향에서는 가능하지 않는(어쩌면 람다를 통해서 가능한데 힘든) function language의 위력을 깨닫는 공부였음.