{"action":"reopened","issue":{"url":"https://api.github.com/repos/hoperyy/blog/issues/104","repository_url":"https://api.github.com/repos/hoperyy/blog","labels_url":"https://api.github.com/repos/hoperyy/blog/issues/104/labels{/name}","comments_url":"https://api.github.com/repos/hoperyy/blog/issues/104/comments","events_url":"https://api.github.com/repos/hoperyy/blog/issues/104/events","html_url":"https://github.com/hoperyy/blog/issues/104","id":304739288,"number":104,"title":"promise 认知","user":{"login":"hoperyy","id":5757051,"avatar_url":"https://avatars1.githubusercontent.com/u/5757051?v=4","gravatar_id":"","url":"https://api.github.com/users/hoperyy","html_url":"https://github.com/hoperyy","followers_url":"https://api.github.com/users/hoperyy/followers","following_url":"https://api.github.com/users/hoperyy/following{/other_user}","gists_url":"https://api.github.com/users/hoperyy/gists{/gist_id}","starred_url":"https://api.github.com/users/hoperyy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hoperyy/subscriptions","organizations_url":"https://api.github.com/users/hoperyy/orgs","repos_url":"https://api.github.com/users/hoperyy/repos","events_url":"https://api.github.com/users/hoperyy/events{/privacy}","received_events_url":"https://api.github.com/users/hoperyy/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-03-13T12:11:12Z","updated_at":"2018-05-29T06:33:25Z","closed_at":null,"author_association":"OWNER","body":"## promise 用法\r\n\r\n##  基本用法\r\n\r\n```JavaScript\r\nconst something = new Promise(function name(resolve, reject) {\r\n\r\n   // resolve 传入的参数：普通参数 或 promise 对象\r\n    resolve(new Promise(function name(resolve, reject) {\r\n        setTimeout(() => {\r\n            resolve(333);\r\n        }, 3000);\r\n    }));\r\n\r\n   // return 333;  // return 无效\r\n});\r\n\r\nsomething\r\n    .then(function name(params) {\r\n        console.log('aa', params)\r\n\r\n        // then 返回的数据：普通数据 或 promise 对象\r\n        return new Promise(function name(resolve, reject) {\r\n            setTimeout(() => {\r\n                resolve(444);\r\n            }, 3000);\r\n        })\r\n    })\r\n    .then(function name(params) {\r\n        console.log('bb', params)\r\n    })\r\n    .catch(function name(err) {\r\n        console.log('!!!err', err)\r\n    })\r\n```\r\n\r\n1. `new Promise(...)` 中必须使用 resolve 传递数据，可以是 普通数据 或 promise 对象，不能使用 return，否则无效\r\n2. `then()` 中使用 `return` 传递数据，可以是普通数据 或 promise 对象\r\n3. 如果设置 `then/catch` 的时候 promise 对象的状态已经改变，仍然会触发\r\n\r\n```JavaScript\r\nconst something = new Promise(function name(resolve, reject) {\r\n    throw Error('hahaha');\r\n});\r\n\r\nsetTimeout(() => {\r\n    something\r\n        .then(function name(params) {\r\n            console.log('a', params)\r\n        })\r\n        .then(function name(params) {\r\n            console.log('b', params)\r\n        })\r\n        .catch(function name(err) {\r\n            console.log('!!!err', err) // catch 仍然会触发\r\n        })\r\n}, 3000);\r\n```\r\n"}}
{"actor":{"display_login":"hoperyy"}}