๐ JavaScript Promises์ JavaScript Async / Await์ ๋ณด๊ณ ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค.
- ์ฝ๋ฐฑ ๋ฐฉ์๊ณผ ๋น์ทํ์ง๋ง,
then๊ณผcatch๋ฅผ ์ฌ์ฉํ์ฌ ๋ ๊ฐ๊ฒฐํ๊ฒ ํํํ ์ ์๋ค.
letp=newPromise((resolve,reject)=>{leta=1+1;if(a===2){// resolveresolve("Success");}else{// rejectreject("Failed");}})// resolvep.then((message)=>{console.log("This is in the then "+message);// reject}).catch((message)=>{console.log("This in in the catch "+message);})// This is in the then Success- ๋น๋๊ธฐ ๋ฐ์ดํฐ ์ฒ๋ฆฌ์ ๊ฐ์ด ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์๊ฐ์ด ๊ฑธ๋ฆฌ๋ ์์
์ Promise๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
- Promise๋ ๊ณ์ ์ค์ฒฉ๋๋ ์ฝ๋ฐฑ ๋ฐฉ์์ ๋จ์ (์ฝ๋ฐฑ ์ง์ฅ)์ ํด๊ฒฐํ ์ ์๋ค.
constuserLeft=false;constuserWatchingOtherChannel=false;functionwatchTutorialCallback(callback,errorCallback){if(userLeft){errorCallback({name: "User Left -",message: "๐"})}elseif(userWatchingOtherChannel){errorCallback({name: "userWatchingOtherChannel -",message: "myChannel < otherChannel"})}else{callback("Thumbs up and Subscribe")}}watchTutorialCallback((message)=>{// ์ฒซ๋ฒ์งธ ์ฝ๋ฐฑ(์ฑ๊ณต)console.log("Success: "+message)},(error)=>{// ๋๋ฒ์งธ ์ฝ๋ฐฑ(์๋ฌ)console.log("Error: "+error.name+" "+error.message)})constuserLeft=false;constuserWatchingOtherChannel=false;functionwatchTutorialPromise(){returnnewPromise((resolve,reject)=>{if(userLeft){reject({name: "User Left -",message: "๐"})}elseif(userWatchingOtherChannel){reject({name: "userWatchingOtherChannel -",message: "myChannel < otherChannel"})}else{resolve("Thumbs up and Subscribe")}})}watchTutorialPromise().then((message)=>{// resolveconsole.log("Success: ")returnmessage}).then((message)=>{console.log(message+" ๐")}).catch((error)=>{// rejectconsole.log("Error: "+error.name+" "+error.message)})- ํญ์
resolveํ๋ ๋ค์์Promise์ ๊ฒฝ์ฐ
Promise.all: ๋ชจ๋Promise๊ฐ ์๋ฃ๋ ํ ํธ์ถ
constrecordVideoOne=newPromise((resolve,reject)=>{resolve("Video 1 Recorded")})constrecordVideoTwo=newPromise((resolve,reject)=>{resolve("Video 2 Recorded")})constrecordVideoThree=newPromise((resolve,reject)=>{resolve("Video 3 Recorded")})Promise.all([recordVideoOne,recordVideoTwo,recordVideoThree]).then((messages)=>{console.log(messages)// ๋ชจ๋ ๋ฐฐ์ด ํํ๋ก ๋ฉ์ธ์ง ์ถ๋ ฅ})// ["Video 1 Recorded", "Video 2 Recorded", "Video 3 Recorded"]Promise.race: ์ฒซ๋ฒ์งธPromise๊ฐ ์๋ฃ๋์๋ง์ ํธ์ถ
constrecordVideoOne=newPromise((resolve,reject)=>{resolve("Video 1 Recorded")})constrecordVideoTwo=newPromise((resolve,reject)=>{resolve("Video 2 Recorded")})constrecordVideoThree=newPromise((resolve,reject)=>{resolve("Video 3 Recorded")})Promise.race([// ์ฒซ๋ฒ์งธ ์์์ธ recordVideoOne์ด ์๋ฃ๋๋ฉด ํธ์ถrecordVideoOne,recordVideoTwo,recordVideoThree]).then((message)=>{console.log(message)// ํ๋์ ๋ฉ์ธ์ง ์ถ๋ ฅ})// Video 1 Recorded
Async/ Await์ Promise๋ฅผ ๋ ํธ๋ฆฌํ๊ฒ(๋๊ธฐ์ ์ผ๋ก) ์ฌ์ฉํ๊ธฐ ์ํด ๋ง๋ค์ด์ง ๋ฌธ๋ฒ์ ์ธ ์คํ
functionmakeRequest(location){returnnewPromise((resolve,reject)=>{console.log(`Making Request to ${location}`)if(location==="Google"){resolve("Google says hi")}else{reject("We can only talk to Google")}})}functionprocessRequest(response){returnnewPromise((resolve,reject)=>{console.log("Processing response")resolve(`Extra Information + ${response}`)})}constLOCATION="Google";// const LOCATION = "Facebook";makeRequest(LOCATION).then(response=>{console.log("Response Received")// console.log(response) // Google says hireturnprocessRequest(response)}).then(processResponse=>{console.log(processResponse)}).catch(error=>{console.log(error)})/* resolveMaking Request to GoogleResponse ReceivedProcessing responseExtra Information + Google says hi*//* rejectMaking Request to FacebookWe can only talk to Google*/๐ Async/ Await ์ ์์
- ํญ์ ์ฝ๋๋ฅผ ํจ์๋ก ๊ฐ์ธ๊ณ ํจ์ ์์
asyncํค์๋๋ฅผ ์ถ๊ฐํ๋ค. - ๋น๋๊ธฐ ์ฝ๋ ์์
awaitํค์๋ ์ฌ์ฉํ์ฌPromise์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ค.
functionmakeRequest(location){returnnewPromise((resolve,reject)=>{console.log(`Making Request to ${location}`)if(location==="Google"){resolve("Google says hi")}else{reject("We can only talk to Google")}})}functionprocessRequest(response){returnnewPromise((resolve,reject)=>{console.log("Processing response")resolve(`Extra Information + ${response}`)})}// const LOCATION = "Google"; constLOCATION="Facebook";asyncfunctiondoWork(){try{constresponse=awaitmakeRequest(LOCATION)// Google says hiconsole.log("Response Received")constprocessResponse=awaitprocessRequest(response)console.log(processResponse)}catch(error){console.log(error)}}doWork()/* resolveMaking Request to GoogleResponse ReceivedProcessing responseExtra Information + Google says hi*//* rejectMaking Request to FacebookWe can only talk to Google*/