Kimn
    • 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
    --- ###### tags: `Game` --- # 11/30 - Promise、async/await ## Callback function 回調函式 ### 什麼是 callback? - callback 就是將 function 當作參數傳入另外一個函式中 ### JavaScript 的同步與非同步 - 同步:就像==接力賽跑==,當棒子沒有交給我,我就得等你,不能跑 - 非同步:就像一般我們在點餐時,服務生會給號碼牌,我就==可以離開櫃台先去逛街==,櫃台也可以繼續為下一個客人點餐,接著只要等號碼牌響起,我再去取餐即可。 ```typescript= // 定義 callback 是一個 function(string):void function main(callback: (data: string) => void) { const data: string = "Hello"; callback(data); } // 參數 a,b 都是從 main 裡面丟進去的 main(function (a: string) { console.log(a); // Hello }); main((b: string) => { const test = b + "測試"; console.log(b); // Hello console.log(test); // Hello測試 }); ``` ## Promise ### 概述 :::info - 你可以想像你去買橘子,結果店裡沒有進貨,店員對你==Promise==,只要他店裡到貨(fulfilled)或者不再進貨(rejected),==then==他就會通知你 ::: - Promise 是一個構造函數,new Promise 返回一個 promise 物件,接收一個帶有 resolve 和 reject 兩個參數的函數,這個函數在 Promise 構造函數返回所創建 promise 實例對象前被調用。 - ==resolve,reject 是一個函數==,處理結束後調用 resolve 或 reject。 - 當調用 resolve,會把當前 promise 對象狀態由 pending 標記成功(fulfilled),當調用 reject,會把狀態由 pending 標記失敗(rejected)。 - - [參考資料](https://segmentfault.com/a/1190000025180588) ```typescript= // 接收兩個 function 成功-> then ,失敗-> catch const promise = new Promise((resolve, reject) => { // 執行後即回傳,不會往下 reject("失敗"); resolve("成功"); }); promise .then((res) => { console.log(res); // 成功 }) .catch((err) => { console.log(err); // 失敗 - 先回傳才讀得到值 }); ``` - Promise.all ( ) 並發處理多個異步任務,所有任務都執行完成才執行回調函數 - Promise.race ( ) 只要有一個任務完成就會執行回調得到結果 ```typescript= const p1 = new Promise((resolve, reject) => { resolve(1); }); const p2 = new Promise((resolve, reject) => { resolve(2); }); const p3 = new Promise((resolve, reject) => { resolve(3); }); Promise.all([p1, p2, p3, promise]).then((res) => { console.log(res); // [ 1, 2, 3 ] }); Promise.race([promise, p1, p2, p3]).then((res) => { console.log("有一個異步率先完成了", res); //有一個異步率先完成了 成功 }); ``` ### Promise 的存在意義 ```typescript= // Promise() 同步 executor 同步 const p = new Promise((resolve, reject) => {}); // 異步 p.then((res) => {}); ``` :::info 問題:為什麼 Promise 執行是同步,p.then 是異步 ? ::: - AJAX 為例 ```typescript= // 異步程序 let data = $.ajax({ url: "https://hopsell-api.herokuapp.com/product/all", async: false, // 改成同步 }); // 會等 1~2 秒資料回傳才繼續往下 console.log(getNames(data.responseJSON.data)); console.log("我看見很多商品"); function getNames(data: any[]) { return data.map((item) => { return item.productName; }); } ``` - 雖然成功將 getNames() 從 AJAX 中抽離,但也造成了後續的程式阻塞 - 顯然不符合我們的需求 ```typescript= // Promise 異步問題同步化解決方案 - 最優解 const promise = new Promise((resolve, reject) => { $.ajax({ url: "https://hopsell-api.herokuapp.com/product/all", success(data) { resolve(data); }, }); }); // 異步 promise.then((res: any) => { console.log(getNames(res.data)); }); console.log("我看見很多商品"); function getNames(data: any[]) { return data.map((item) => { return item.productName; }); } ``` ## Generator - generator 和函數不同的是,generator 由 function* 定義(注意多出的*號) - 並且除了 return 語句,還可以用 yield 返回多次。 ```typescript= // 正常情況 function fib(max: number) { let a = 0, b = 1, arr = [0, 1]; while (arr.length < max) { [a, b] = [b, a + b]; arr.push(b); } return arr; } console.log(fib(5)); // [ 0, 1, 1, 2, 3 ] - 回傳陣列 // 採用 Generator function* fib2(max: number) { let a = 0, b = 1, n = 0; while (n < max) { yield a; [a, b] = [b, a + b]; n++; } return; } let f = fib2(5); f.next(); // { value: 0, done: false } - next()後執行一次 f.next(); // { value: 1, done: false } f.next(); // { value: 1, done: false } f.next(); // { value: 2, done: false } ``` ## async / await #### 訣竅(Tips) - 在迴圈中使用 async/await - 若想要在迴圈中使用 async / await 的寫法需要特別留意, 一般來說在 for, while 或 for...of 這種迴圈中都可以正常使用;但若是在帶有 callback 的迴圈中,例如 forEach, map, filter, reduce 等等,就需要特別留意: - 如果想要在迴圈中使用 await,則==可以使用一般的 for 迴圈、for await...of,或 map 搭配 Promise 的寫法==,==千萬不要使用 forEach==。 - 若有需要使用 filter 或 reduce 等其他處理陣列的方式,==先等陣列的資料完整取得後再來呼叫這些方法。==

    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