--- tags: Node.js 直播班 - 2023 春季班 --- # 🏅 Day 2 - 非同步概念、使用 Promise ### 使用 Promise 簡介 Promise 是一個物件,代表一個即將完成或失敗的非同步操作,以及回傳的值 建立 Promise 的寫法為: ```javascript= function example() { new Promise((resolve, reject) => { // 執行一些非同步作業,最終呼叫: // // resolve(someValue); // 實現 // 或 // reject("failure reason"); // 拒絕 } ) } ``` 建立好 Promise 後,執行此函式就可以在完成 Promise 時使用 `.then()` 接收回傳的 Promise 物件,以及使用 `.catch()` 接收錯誤,因 Promise 可以用鏈式寫法接收上一個 Promise 的回傳值,不需像使用 callback function 一層包一層,因此可以提昇易讀性 ```javascript= example() .then(data => useData(data) ) .catch(error => console.log(error) ) ``` ### 參考資源 - [使用 Promises 來處理非同步操作](https://developer.mozilla.org/zh-TW/docs/Learn/JavaScript/Asynchronous/PromisesTW/docs/Learn/JavaScript/Asynchronous/Introducing) - [JavaScript Promise 全介紹 | 卡斯伯 Blog - 前端,沒有極限](https://www.casper.tw/development/2020/02/16/all-new-promise/) - 課程影音「Node.js 非同步技巧 - Promise、Async、Await」 ### 題目 請嘗試使用 Promise 的鏈式寫法 `.then()` `.catch()` 執行以下函式 **執行流程**: 批改作業 → 檢查獎勵 → 給予獎勵 `.then()` → 退學或懲罰 `.catch()` ```javascript // 批改作業 function correctTest(name) { return new Promise((resolve, reject) => { setTimeout(() => { const score = Math.round(Math.random()*100); if(score >= 20) { resolve({ name, score }) } else { reject("您已達退學門檻") } }, 2000) }) } // 檢查獎勵 function checkReward(data) { return new Promise((resolve, reject) => { setTimeout(() => { if(data.score >= 90) { resolve(`${data.name} 獲得電影票`); } else if (data.score >= 60 && data.score < 90) { resolve(`${data.name} 獲得嘉獎`); } else { reject(`您沒有獎品`) } }, 2000) }) } // 執行函式 /* 在此填寫答案*/ ``` ## 回報流程 將答案寫在 CodePen 並複製 CodePen 連結貼至底下回報就算完成了喔! 解答位置請參考下圖(需打開程式碼的部分觀看)  <!-- 解答: correctTest("小明") .then(data => checkReward(data)) .then(reward => console.log(reward)) .catch(error => console.log(error)) --> 回報區 --- | 報數 | 組別/Discord 名字 | Codepen/其他回饋 | |:----:|:-------------------:|:---------------------------------------------------------------------------------------------------------------------------------:| | 1 | 北 1 組 / PittWu | [Blog Post - Day 2](https://pitt-wu-blog.vercel.app/docs/day2-then-catch) | | 2 | 北 10 組 / Benson | [Github Benson - Day 2](https://github.com/ioveasdkre/HexschoolOperation/tree/main/NodejsEnterpriseClass/day40-tasks/day2/app.ts) | | 3 | 北 6 組 /pamiya | [codepen - Day 2](https://codepen.io/chiakilalala/pen/BaOJWQL?editors=1111) | | 4 | 北 5 組 / 圈圈 | [Codepen - Day2](https://codepen.io/wjejfczn-the-bold/pen/XWPZRJG?editors=1111) | | 5 | 南 2 組 / Duke | [Codepen - Day 2](https://codepen.io/DukeTian/pen/MWqQVyY?editors=0011) | | 6 | 北 1 組 / MayYi | [Codepen - Day2](https://codepen.io/mayyang/pen/ZEMRzJx?editors=0011) | | 7 | 北 8 組 / Zhao Chen | [GitHub - Day 2](https://github.com/zhao1995chen/NodejsEnterpriseClass/blob/master/daily-task/day2/index.ts) | | 8 | 南 2 組 / LEONARD | [Codepen - Day2](https://codepen.io/clps935419/pen/qBMJyJw?editors=0010) | | 9 | 北 3 組 / erik1110 | [GitHub - Day 2](https://github.com/erik1110/NodejsEnterpriseClass/blob/main/daily-task/day2/index.ts) | | 10 | 南 2 組 / fengyao | [GitHub - Day 2](https://github.com/fengyao33/sixfeet/commit/dd17674b9d70ae94453fef3676ae3e64536ddfe8) | | 11 | 北 11 組 / eddie | [codepen - Day 2](https://codepen.io/eddie605134/pen/wvEQwdN?editors=1111) | | 12 | 北 8 組 / Ryder | [codepen - Day 2](https://codepen.io/rider159159/pen/RwYqwom?editors=1010) | | 13 | 中 3 組 / Wendy | [codepen - Day 2](https://codepen.io/wendy03/pen/eYLQRQM) | | 14 | 中 4 組 / jimkk159 | [codepen - Day 2](https://codepen.io/jimkk159/pen/abaQJvM) | | 15 | 北 16 組 / 啊培培 | [GitHub - Day 2](https://github.com/LABIBI-LG/Courses/blob/main/hexschool/nodeJS/Live_Course/Daily_Tasks/day2/main.js) | | 16 | 北 12 組 / Sam Lin | [GitHub - Day 2](https://github.com/samlin1122/hex-school/blob/main/daily-challenges/day2.js) | | 17 | 北 16 組 / Zoe Wu | [GitHub - Day 2](https://github.com/Zoe561/hexschool/tree/master/nodejs-enterprise-class/daily-task/day2/app.js) | | 18 | 北 8 組 / Ryan | [codepen - Day 2](https://codepen.io/chingyuan/pen/xxaMLEa) | | 19 | 南 2 組 / Kai | [codepen - Day 2](https://codepen.io/Shung-Kai/pen/JjazQmN?editors=1012) | | 20 | 北 17 組 / Chian | [codepen - Day 2](https://codepen.io/chian-yang/pen/LYJvbmY) | | 21 | 北 16 組 / 文文 | [codepen - Day 2](https://codepen.io/chiawen81/pen/MWqdxJz?editors=0011) | | 22 | 南 1 組 / hiYifang | [codepen - Day 2](https://codepen.io/hiYifang/pen/wvpXOGW) | | 23 | 北 13 組 / Louisa | [GitHub - Day 2](https://github.com/louisa0416/NodejsEnterpriseClass/tree/master/daily-task/day02) | | 24 | 北 13 組 / sasha | [codepen - Day 2](https://codepen.io/sasay/pen/ZEMdPZZ) | |25 | 南 1 組 / Christina | [Codepen - Day 2](https://codepen.io/chrisc0210/pen/PodMROG)| |26|中 3 組 / Jill|[codepen - Day 1](https://codepen.io/chi-ling-lee/pen/LYgYvNK?editors=0012)| |27|北5組/albee|[GitHub-Day 2](https://github.com/albee-chang/hexschool-node-dailytask/tree/main/day2)| |28|北 1 組 / Asher|[Notion - Day 2](https://wh-lin.notion.site/Day2-Promise-92bf41b2e8ca4643880387a658d03e8e)| |29|中 4 組 / Zoey|[codepen - Day 2](https://codepen.io/jsbo12551a/pen/mdzPzqd?editors=0012)|
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up