Jamie Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 約定(Promise) [![hackmd-github-sync-badge](https://hackmd.io/1UXaG3sMSd-39oYkq163wA/badge)](https://hackmd.io/1UXaG3sMSd-39oYkq163wA) ## Promise簡介 ### 功能: - 主要是用來簡化非同步事件之間的嵌套(避免回調地獄) ### 建構Promise物件: - Promise建構式建立同時,必須傳入一個函式作為參數,此函式被稱為執行器函式(executor function) - 執行器函式在被建構的當下就會立即被觸發執行 - 執行器函式的參數包含resolve和reject兩個函式 - resolve(必選)代表的是成功的完成此約定(Promise事件) - reject(可選)代表的是此約定中發生錯誤 - resolve和reject僅能運行其中一個,且只能運行一次,回傳後代表此約定(Promise事件)結束 - resolve函式被運行(呼叫),代表此Promise事件結束,並進入「已實現」狀態(resolved) - reject函式被運行(呼叫),代表此Promise事件結束,並進入「被拒絕」狀態(rejected) ### 三種返回狀態: - pending: 事件還在運行中,尚未取得結果 - resolved、fulfilled: 事件已經執行完畢且成功操作,並回傳resolve函式的結果 - rejected: 事件已經執行完畢但操作失敗,並回傳reject函式的結果 ### .then方法 - 是一個Promise物件的內建方法 - .then會在上一層的Promise事件結束後被呼叫 - .then可帶入兩個回調函式(函式名可自取),兩者分別又可以帶入各自的參數 - onFulfilled(必選): 在成功(Fulfilled)時執行的函式,此函式帶入的參數為 Promise 函式中 resolve 所帶入的值 - onRejected(可選): 在失敗(Rejected)時執行的函式,此函式帶入的參數為 Promise 函式中 reject 所帶入的值 - 結束此次Promise事件: - 回傳(return)一個值,代表此Promise事件結束,並進入「已實現」狀態(resolved) - 拋出(throw)一個例外,代表此Promise事件結束,並進入「被拒絕」狀態(rejected) ### .catch方法 - 是一個Promise物件的內建方法 - 參數為一個回調函式 - 當前面的.then有寫onRejected函式時,發生rejected會先進到.then,而不是.catch - 當前面的.then都沒有寫onRejected函式時,發生rejected則會跳到.catch,此回調函式帶入的參數值同樣為 Promise 函式中rejected狀態所返回的值 ### .finally方法 - 是一個Promise物件的內建方法 - 回調函式不帶參數 - 通常用來關閉連線(EX:資料庫)等操作 ### 流程圖 ![](https://i.imgur.com/PP64SI7.png) &emsp; ## 使用範例 ### 定義回調函數(onFulfilled, onRejected) - 也可以直接寫在裡面,但為了好閱讀,所以提出來 ```javascript= function onFulfilled(res){ console.log("(Fulfilled)第一層.then:", res); return res }; function onRejected(res){ console.log("(Rejected)第一層.then:", res); throw res; }; ``` ### 主要流程: ```javascript= // resolve,reject 參數名可自定義,但為了好區分通常不會亂取 let p = new Promise(function(resolve, reject){ const judge = Math.random() > 0.5 ? true : false; if(judge){ // 正確完成的回傳方法 resolve('Promise成功'); } else { // 失敗的回傳方法 reject('Promise失敗'); }; }) /* .then: - .then會在上一層的Promise事件結束後被呼叫 - .then可帶入兩個回調函式(函式名可自取),兩者分別又可以帶入各自的參數 - onFulfilled(必選): 在成功(Fulfilled)時執行的函式,此函式帶入的參數為 Promise 函式中 resolve 所帶入的值 - onRejected(可選): 在失敗(Rejected)時執行的函式,此函式帶入的參數為 Promise 函式中 reject 所帶入的值 - 結束此次Promise事件: - 回傳(return)一個值,代表此Promise事件結束,並進入「已實現」狀態(resolved) - 拋出(throw)一個例外,代表此Promise事件結束,並進入「被拒絕」狀態(rejected) */ .then(onFulfilled, onRejected) /* 可以一直.then下去, - 當上層進入resolved時,也就是使用了return,表示上層Promise事件結束,且會進入到下層Promise事件,並被onFulfilled函式接收到 - return的回傳值會變成下層onFulfilled函式的參數值 - 當上層進入rejected時,也就是使用了throw,表示上層Promise事件結束,且會進入到下層Promise事件,並被onRejected函式接收到 - throw的錯誤值會變成下層onRejected函式的參數值 */ .then(function(res){ // 只有onFulfilled函式,所以當上層Rejected時,會被後面.then中的onRejected函式接收到或是直接進入到.catch console.log("(Fulfilled)第二層.then:", res); return res; }) .then((res) => { console.log("(Fulfilled)第三層.then:", res); return res; }, (error) => { console.log("(Rejected)第三層.then:", error); throw error; }) /* .catch: - 參數為一個回調函式, - 當前面的.then有寫onRejected函式時,發生rejected會先進到.then,而不是.catch - 當前面的.then都沒有寫onRejected函式時,發生rejected則會跳到.catch,此回調函式帶入的參數值同樣為 Promise 函式中rejected狀態所返回的值 */ .catch(function(error){ console.log(".catch:", error); }) /* .finally中的回調函式不帶參數 - 通常用來關閉讀取等操作 */ .finally(function(){ console.log("finally"); }); ``` &emsp; ## 其他Promise方法 ### Promise.all: - 透過陣列的形式傳入多個 promise 函式 - 全都Fulfilled才會在全部執行完成後回傳陣列結果,陣列的結果順序與一開始傳入的一致 - 只要有一個Rejected就會執行onRejected或是.catch - 適合用在多支 API 要一起執行,並確保全部完成後才進行其他工作時 - 範例: ```javascript= function promise(num, time = 500) { let p = new Promise((resolve, reject) => { // setTimeout()的作用是在延遲了某段時間(單位為毫秒)之後,才去執行「一次」指定的程式碼 setTimeout(() => { if(num >= 1){ resolve(`${num}, 成功`) }else{ reject('失敗'); }; }, time); }); return p; }; Promise.all([promise(1, 3000), promise(2), promise(3)]) .then(onFulfilled, onRejected) .catch((error) => { console.log(error); }); ``` ### Promise.race: - 透過陣列的形式傳入多個promise函式,在全部執行完成後,回傳第一個運行完成的結果 - 用在站點不穩定,同時發送多支同行為 API 確保可行性使用,但實作中使用率並不高 - 範例: ```javascript= function promise(num, time = 500) { let p = new Promise((resolve, reject) => { // setTimeout()的作用是在延遲了某段時間(單位為毫秒)之後,才去執行「一次」指定的程式碼 setTimeout(() => { if(num >= 1){ resolve(`${num}, 成功`) }else{ reject('失敗'); }; }, time); }); return p; }; Promise.race([promise(0), promise(2), promise(3, 3000)]) .then(onFulfilled, onRejected) .catch((error) => { console.log(error); }); ``` ### Promise.resolve: - 一定回傳resolve - 參數為回傳值 - 範例: ```javascript= function onFulfilled(res){ console.log('In onFulfilled:', res); return res }; function onRejected(res){ console.log('In onRejected:', res); return res }; Promise.resolve('result') .then(onFulfilled, onRejected); ``` ### Promise.reject: - 一定回傳reject - 參數為回傳值 ```javascript= function onFulfilled(res){ console.log('In onFulfilled:', res); return res }; function onRejected(res){ console.log('In onRejected:', res); return res }; Promise.reject('result') .then(onFulfilled, onRejected); ``` &emsp; ## Promise搭配Async/Await ### 簡介: - Async - 用來裝飾函數,使它變成一個異步函數 - async函數返回的是一個Promise對象,因此也可以使用.then方法 - 如果async函式回傳了一個值,Promise的狀態將為一個帶有該回傳值的resolved - 如果async函式拋出例外或某個值,Promise的狀態將為一個帶有被拋出值的rejected - Await - 只能用於異步函數中 - 使用await會暫停此async函數的執行,並且等待傳遞至表達式的Promise的解析,解析完之後會回傳解析值,並繼續此async函數的執行 - 使用await時,最好用try...catch包起來,因為有可能會是rejected ### 範例: ```javascript function promise(num, timeout=500) { let p = new Promise((resolve, reject) => { // setTimeout()的作用是在延遲了某段時間(單位為毫秒)之後,才去執行「一次」指定的程式碼 setTimeout(() => { resolve(`${num}成功`) }, timeout); }); return p; }; async function run(){ try { const result_with_no_await = Promise.all([promise(1, 3000), promise(2), promise(3)]) .then(([res1, res2, res3]) => { return [res1, res2, res3]; }); // 因為沒有使用await關鍵字去等待,result_with_no_await的結果還沒出來,所以會返回 Promise { <pending> } console.log(result_with_no_await); const result_with_await = await Promise.all([promise(1, 3000), promise(2), promise(3)]) .then(([res1, res2, res3]) => { return [res1, res2, res3]; }); // 因為有使用await關鍵字,所以程序會等result_with_await回傳後再繼續進行,有接收到值[ '1成功', '2成功', '3成功' ] console.log(result_with_await); } catch (error) { console.log(error); } }; run(); ``` ###### tags: `JavaScript`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    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

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully