{"action":"opened","issue":{"url":"https://api.github.com/repos/zzly00/mentor-program-2nd-blog/issues/25","repository_url":"https://api.github.com/repos/zzly00/mentor-program-2nd-blog","labels_url":"https://api.github.com/repos/zzly00/mentor-program-2nd-blog/issues/25/labels{/name}","comments_url":"https://api.github.com/repos/zzly00/mentor-program-2nd-blog/issues/25/comments","events_url":"https://api.github.com/repos/zzly00/mentor-program-2nd-blog/issues/25/events","html_url":"https://github.com/zzly00/mentor-program-2nd-blog/issues/25","id":376682058,"node_id":"MDU6SXNzdWUzNzY2ODIwNTg=","number":25,"title":"[筆記] Callback function","user":{"login":"zzly00","id":31249335,"node_id":"MDQ6VXNlcjMxMjQ5MzM1","avatar_url":"https://avatars1.githubusercontent.com/u/31249335?v=4","gravatar_id":"","url":"https://api.github.com/users/zzly00","html_url":"https://github.com/zzly00","followers_url":"https://api.github.com/users/zzly00/followers","following_url":"https://api.github.com/users/zzly00/following{/other_user}","gists_url":"https://api.github.com/users/zzly00/gists{/gist_id}","starred_url":"https://api.github.com/users/zzly00/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zzly00/subscriptions","organizations_url":"https://api.github.com/users/zzly00/orgs","repos_url":"https://api.github.com/users/zzly00/repos","events_url":"https://api.github.com/users/zzly00/events{/privacy}","received_events_url":"https://api.github.com/users/zzly00/received_events","type":"User","site_admin":false},"labels":[{"id":1048711821,"node_id":"MDU6TGFiZWwxMDQ4NzExODIx","url":"https://api.github.com/repos/zzly00/mentor-program-2nd-blog/labels/note","name":"note","color":"8cbeaa","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-11-02T06:11:25Z","updated_at":"2018-11-02T06:11:25Z","closed_at":null,"author_association":"OWNER","body":"- 在使用 callback function 前，原本可能會這樣寫\r\n```javascript\r\n// 同步 or 阻塞 block\r\nconst result = getDate()\r\nconsole.log(result) // 一定要等到取得 result，才會繼續執行\r\n```\r\n- 如果 getDate() 執行五分鐘，則需要等待，才會繼續執行下面的程式碼\r\n\r\n## Callback 好了叫我\r\n```javascript\r\nfunction callMe(data){  // ... 收到 cb 給的 response\r\n    console.log(data)\r\n}\r\n\r\ngetDate(callMe)\r\n\r\nfunction getDate(cb){  // 傳一個function\r\n    ... 發 request\r\n    ... response 回來\r\n    cb(response)  // call cb\r\n}\r\n\r\n\r\n// anonymous function\r\ngetDate(id, function(data){  // 若需要帶 function 以外的參數，則直接加，例如：id\r\n    console.log(data)\r\n})\r\n\r\nfunction getDate(id, cb){  // 傳一個function\r\n    ... 發 request id\r\n    ... response 回來\r\n    cb(response)  // call cb\r\n}\r\n```\r\n\r\n## setTimeout\r\n```javascript\r\nsetTimeout(cb, 3000)  // 3秒後執行 cb\r\n\r\nfunction cb(){\r\n    console.log('yo')\r\n}\r\n```\r\n\r\n參考資料：程式導師實驗計畫第二期：Week4-2 - JavaScript"}}
{"actor":{"display_login":"zzly00"}}