欢迎来到258分享网,纯净的网络源码分享基地!

258资源分享网

全部作品
全部作品
网站源码
微信源码
素材特效
源码插件
视频教程
建站学院
热门搜索: 织梦  农业种植  农业  安全设置  官方
258资源分享 > 建站学院 > 微信开发 > 把微信小程序异步API为Promise,简化异步编程

推荐下载

HTML5响应式自适应网咯设计

2020-05-12   浏览:740

高端HTML5响应式企业通用网

2020-05-06   浏览:521

html5响应式外贸网站英文版

2020-05-08   浏览:510

HTML5自适应律师工作室类网

2020-04-04   浏览:504

HTML5影视传媒文化公司类网

2020-05-12   浏览:500

把微信小程序异步API为Promise,简化异步编程

发布时间:2020-10-12  

把微信小程序异步API转化为Promise。用Promise处理异步操作有多方便,谁用谁知道。
微信官方没有给出Promise API来处理异步操作,而官方API异步的又非常多,这使得多异步编程会层层回调,代码一复杂,回调起来就想砸电脑。
于是写了一个通用工具,把微信官方的异步API转化为Promise,方便处理(多)异步操作。

你可以这样用:

准备转化后的方法并暴露出

// /utils/wx-promise.js import toPromise from '/module/to-promise/src/index' const toPromiseWx = toPromsie(wx) export const request = toPromiseWx('requset') export const getLocation = toPromiseWx('getLocation') export const setStorage = toPromiseWx('setStorage') //export 其他你项目中可能用到的异步API

在其他文件中使用
在App.js中使用:

//App.js import { request } from './utils/wx-promise.js' App({ onLanuch: () => { request({ url: '' }) .then(() => { //成功后处理 }) .then(() => { //失败后处理 }) } })

在其他page中使用:

// /page/index.js import { request, setStorage } from '../utils/wx-promise.js' page({ onLoad: () => { request({ url: '' }) .then(() => { //成功后处理 }) .then(() => { //失败后处理 }) }, onHide: () => { setStorage({ key: 'yourkey', data: 'yourvalue' }) .then(() => { //保存成功 }) .then(() => { //保存失败 }) } })

项目地址:to-promise

其他更多更具体用法,直接粘贴README了,如下。

to-promise是一个转换微信小程序异步API为Promise的一个工具库

优点:

避免小程序异步编程多次回调带来的过多回调导致逻辑不清晰,篇幅过长等问题。

借助于Promise异步编程特点,支持链式操作,像同步一样写异步。

转化后得API几乎和微信官方API一样。

使用方法:

安装

使用git安装到项目根目录/module,

git clone https://github.com/tornoda/to-promise

或直接下载放入项目目录下如:/module

在需要用到的地方引入

import toPromise from '/module/to-promise/src/index'

绑定微信全局对象(wx)到函数,以便可以取到微信得API

const toPromiseWx = toPromise(wx)

开始转化你需要得异步API

//apiName为微信异步方法名,如对wx.request()进行转化 const request = toPromiseWx('request') //直接使用request方法

举例:

import toPromise from '/module/to-promise/src/index' //转换wx.getStorage() const getStorage = toPromsie(wx)('getStorage') //使用 getStorage({ key: 'test' }) .then( (res) => { //res的值与wx.getStorage({ success: (res) => {} })中的res值一样 //res = {data: 'keyValue'} console.log(res.data)//控制台打印storage中key对于的value return res.data//如果需要继续链式调用转化后的api,需要把值显示返回 }, (err) => { //err的值与wx.getStorage({ success: (err) => {} })中的err值一样 throw err } )

关于Promise对象的使用,请参见Promise

API

toPromise(global)

参数

(wx): wx全局对象。即toPromise(wx)这样调用

返回

(function): 参数(string)为小程序异步方法名。返回一个函数,该函数的参数与返回值如下。

参数:(object) 对应wx小程序异步方法中的参数(OBJECT)除去success与fail后的对象。例如:

官方APIwx.getLocation(OBJECT)的OBJECT接受如下属性: type altitude success fail complete,那么去除(success fail)后为:type altitude complete。

返回: (pending Promsise) 返回一个未知状态的Promise对象,在该对象上调用.then(onFulfilled, onRejected)方法来处理对用成功或失败的情况。onFulfilled为请求成功后调用的回调函数,参数为返回值,onRejected为请求失败后的回调函数,参数为返回的错误信息。

简单点来说,

const getLocation = toPromiseWx('getLocation') getLocation({ type: 'wgs84', altitude: true, complete: () => { console.log('to-promsise is awesome') } }).then( (res) => {//dosomething if succeed}, (err) => {//dosomething if failed} )

与下面官方调用等价

wx.getLocation({ type: 'wgs84', altitude: true, complete: () => { console.log('to-promsise is awesome') }, success: (res) => {//dosomething if succeed}, fail: (err) => {//dosomething if failed} })

应用场景举例

单次异步调用,参见API最后