{"id":31288,"date":"2024-11-25T09:18:18","date_gmt":"2024-11-25T01:18:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31288\/"},"modified":"2024-11-25T09:18:18","modified_gmt":"2024-11-25T01:18:18","slug":"%e4%bd%bf%e7%94%a8promise%e7%ae%80%e5%8c%96%e5%9b%9e%e8%b0%83","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31288\/","title":{"rendered":"\u4f7f\u7528Promise\u7b80\u5316\u56de\u8c03"},"content":{"rendered":"<p>\u5728\u9879\u76ee\u4e2d\uff0c\u4f1a\u51fa\u73b0\u5404\u79cd\u5f02\u6b65\u64cd\u4f5c\uff0c\u5982\u679c\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u56de\u8c03\u91cc\u8fd8\u6709\u5f02\u6b65\u64cd\u4f5c\uff0c\u5c31\u4f1a\u51fa\u73b0\u56de\u8c03\u91d1\u5b57\u5854\u3002<\/p>\n<h2>\u6bd4\u5982\u4e0b\u9762\u8fd9\u79cd<\/h2>\n<pre>\/\/ \u6a21\u62df\u83b7\u53d6code\uff0c\u7136\u540e\u5c06code\u4f20\u7ed9\u540e\u53f0\uff0c\u6210\u529f\u540e\u83b7\u53d6userinfo\uff0c\u518d\u5c06userinfo\u4f20\u7ed9\u540e\u53f0\r\n\/\/ \u767b\u5f55\r\nwx.login({\r\n    success: res =&gt; {\r\n        let code = res.code\r\n        \/\/ \u8bf7\u6c42\r\n        imitationPost({\r\n            url: '\/test\/loginWithCode',\r\n            data: {\r\n                code\r\n            },\r\n            success: data =&gt; {\r\n                \/\/ \u83b7\u53d6userInfo\r\n                wx.getUserInfo({\r\n                    success: res =&gt; {\r\n                        let userInfo = res.userInfo\r\n                        \/\/ \u8bf7\u6c42\r\n                        imitationPost({\r\n                            url: '\/test\/saveUserInfo',\r\n                            data: {\r\n                                userInfo\r\n                            },\r\n                            success: data =&gt; {\r\n                                console.log(data)\r\n                            },\r\n                            fail: res =&gt; {\r\n                                console.log(res)\r\n                            }\r\n                        })\r\n                    },\r\n                    fail: res =&gt; {\r\n                        console.log(res)\r\n                    }\r\n                })\r\n            },\r\n            fail: res =&gt; {\r\n                console.log(res)\r\n            }\r\n        })\r\n    },\r\n    fail: res =&gt; {\r\n        console.log(res)\r\n    }\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2>\u4e0b\u9762\u5206\u6790\u5982\u4f55\u7528Promise\u6765\u8fdb\u884c\u7b80\u5316\u4ee3\u7801<\/h2>\n<p>\u56e0\u4e3a\u5fae\u4fe1\u5f02\u6b65api\u90fd\u662fsuccess\u548cfail\u7684\u5f62\u5f0f\uff0c\u6240\u6709\u6709\u4eba\u5c01\u88c5\u4e86\u8fd9\u6837\u4e00\u4e2a\u65b9\u6cd5:<\/p>\n<p><strong><em>promisify.js<\/em><\/strong><\/p>\n<pre>module.exports = (api) =&gt; {\r\n    return (options, ...params) =&gt; {\r\n        return new Promise((resolve, reject) =&gt; {\r\n            api(Object.assign({}, options, { success: resolve, fail: reject }), ...params);\r\n        });\r\n    }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5148\u770b\u6700\u7b80\u5355\u7684:<\/p>\n<pre>\/\/ \u83b7\u53d6\u7cfb\u7edf\u4fe1\u606f\r\nwx.getSystemInfo({\r\n    success: res =&gt; {\r\n        \/\/ success\r\n        console.log(res)\r\n    },\r\n    fail: res =&gt; {\r\n\r\n    }\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f7f\u7528\u4e0a\u9762\u7684promisify.js\u7b80\u5316\u540e:<\/p>\n<pre>const promisify = require('.\/promisify')\r\nconst getSystemInfo = promisify(wx.getSystemInfo)\r\n\r\ngetSystemInfo().then(res=&gt;{\r\n    \/\/ success\r\n    console.log(res)\r\n}).catch(res=&gt;{\r\n\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u7b80\u5316\u540e\u7684\u56de\u8c03\u91cc\u5c11\u4e86\u4e00\u4e2a\u7f29\u8fdb\uff0c\u5e76\u4e14\u56de\u8c03\u51fd\u6570\u4ece9\u884c\u51cf\u5c11\u5230\u4e866\u884c\u3002<\/p>\n<h2>\u56de\u8c03\u91d1\u5b57\u5854\u7684\u7b80\u5316\u6548\u679c<\/h2>\n<p>\u90a3\u4e48\u518d\u6765\u770b\u770b\u6700\u5f00\u59cb\u7684\u90a3\u4e2a\u56de\u8c03\u91d1\u5b57\u5854<\/p>\n<pre>const promisify = require('.\/promisify')\r\nconst login = promisify(wx.login)\r\nconst getSystemInfo = promisify(wx.getSystemInfo)\r\n\r\n\/\/ \u767b\u5f55\r\nlogin().then(res =&gt; {\r\n    let code = res.code\r\n    \/\/ \u8bf7\u6c42\r\n    pImitationPost({\r\n        url: '\/test\/loginWithCode',\r\n        data: {\r\n            code\r\n        },\r\n    }).then(data =&gt; {\r\n        \/\/ \u83b7\u53d6userInfo\r\n        getUserInfo().then(res =&gt; {\r\n            let userInfo = res.userInfo\r\n            \/\/ \u8bf7\u6c42\r\n            pImitationPost({\r\n                url: '\/test\/saveUserInfo',\r\n                data: {\r\n                    userInfo\r\n                },\r\n            }).then(data =&gt; {\r\n                console.log(data)\r\n            }).catch(res =&gt; {\r\n                console.log(res)\r\n            })\r\n        }).catch(res =&gt; {\r\n            console.log(res)\r\n        })\r\n    }).catch(res =&gt; {\r\n        console.log(res)\r\n    })\r\n}).catch(res =&gt; {\r\n    console.log(res)\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u7b80\u5316\u6548\u679c\u975e\u5e38\u660e\u663e\u3002<\/p>\n<p>\u540c\u6837\u9002\u7528\u4e8e\u7f51\u9875\u6216\u8005nodejs\u7b49\u4e2d\u3002<\/p>\n<p>\u76f8\u5173\u63a8\u8350\uff1a<\/p>\n<\/p>\n<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528Promise\u7b80\u5316\u56de\u8c03\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u9879\u76ee\u4e2d\uff0c\u4f1a\u51fa\u73b0\u5404\u79cd\u5f02\u6b65\u64cd\u4f5c\uff0c\u5982\u679c\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u56de\u8c03\u91cc\u8fd8\u6709\u5f02\u6b65\u64cd\u4f5c\uff0c\u5c31\u4f1a\u51fa\u73b0\u56de\u8c03\u91d1\u5b57\u5854\u3002 \u6bd4\u5982\u4e0b\u9762\u8fd9\u79cd \/\/ \u6a21\u62df\u83b7\u53d6code\uff0c\u7136\u540e\u5c06code\u4f20\u7ed9\u540e\u53f0\uff0c\u6210\u529f\u540e\u83b7\u53d6userinfo\uff0c\u518d\u5c06userinfo\u4f20\u7ed9\u540e\u53f0 \/\/ \u767b\u5f55 wx.login({ success: res =&gt; { let code = res.code \/\/ \u8bf7\u6c42 imitationPost({ url: &#8216;\/test\/loginWithCode&#8217;, data: { code }, success: data =&gt; { \/\/ \u83b7\u53d6userInfo wx.getUserInfo({ success: res =&gt; { let userInfo = res.userInfo \/\/ \u8bf7\u6c42 imitationPost({ url: &#8216;\/test\/saveUserInfo&#8217;, data: { userInfo }, success: data =&gt; { console.log(data) }, fail: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31288","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31288"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31288\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}