ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# uniapp 同步上传图片 ![](https://img.kancloud.cn/ad/87/ad876dc476b0e15655f23216a74ab911_828x507.png)\## [Promise 封装](https://uniapp.dcloud.io/api/README?id=promise-%e5%b0%81%e8%a3%85) uni-app 对部分 API 进行了 Promise 封装,返回数据的第一个参数是错误对象,第二个参数是返回数据。 详细策略如下: - 异步的方法,如果不传入 success、fail、complete 等 callback 参数,将以 Promise 返回数据。例如:uni.getImageInfo() - 异步的方法且有返回对象,如果希望获取返回对象,必须至少传入一项 success、fail、complete 等 callback 参数。例如:uni.connectSocket() - 同步的方法(即以 sync 结束),不封装 Promise。例如:uni.getSystemInfoSync() - 以 create 开头的方法,不封装 Promise。例如:uni.createMapContext() - 以 manager 结束的方法,不封装 Promise。例如:uni.getBackgroundAudioManager() 使用示例: ``` <pre class="calibre14">``` <span class="token6">// 默认方式</span> uni<span class="token1">.</span><span class="token3">request</span><span class="token1">(</span><span class="token1">{</span> url<span class="token1">:</span> <span class="token4">'https://www.example.com/request'</span><span class="token1">,</span> success<span class="token1">:</span> <span class="token1">(</span>res<span class="token1">)</span> <span class="token">=></span> <span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>res<span class="token1">.</span>data<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// Promise</span> uni<span class="token1">.</span><span class="token3">request</span><span class="token1">(</span><span class="token1">{</span> url<span class="token1">:</span> <span class="token4">'https://www.example.com/request'</span> <span class="token1">}</span><span class="token1">)</span> <span class="token1">.</span><span class="token3">then</span><span class="token1">(</span>data <span class="token">=></span> <span class="token1">{</span><span class="token6">//data为一个数组,数组第一项为错误信息,第二项为返回数据</span> <span class="token2">var</span> <span class="token1">[</span>error<span class="token1">,</span> res<span class="token1">]</span> <span class="token">=</span> data<span class="token1">;</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>res<span class="token1">.</span>data<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span> <span class="token6">// Await</span> <span class="token2">function</span> <span class="token2">async</span> <span class="token3">request</span> <span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">var</span> <span class="token1">[</span>error<span class="token1">,</span> res<span class="token1">]</span> <span class="token">=</span> <span class="token2">await</span> uni<span class="token1">.</span><span class="token3">request</span><span class="token1">(</span><span class="token1">{</span> url<span class="token1">:</span> <span class="token4">'https://www.example.com/request'</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>res<span class="token1">.</span>data<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ```