pp电子

登录
免费开通

把微信小程序异步API转为Promise,,简化异步编程,,离别层层回调 ...

 把微信小程序异步API转为Promise,,简化异步编程,,离别层层回调 ...  把微信小程序异步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: 'http://api.yourapi.com' })

  .then(() => {

  //乐成后处理

  })

  .then(() => {

  //失败后处理

  })

  }

  })

  在其他page中使用:

  // /page/index.js

  import { request, setStorage } from '../utils/wx-promise.js'

  page({

  onLoad: () => {

  request({ url: 'http://api.yourapi.com' })

  .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最后

  多次异步操作挪用,,且每下一次挪用都会用到前一次返回的效果。。

  如:获得GPS信息后,,凭证GPS信息获取天气信息,,取得天气信息后立马存入localStorage。。

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

  const toPromiseWx = toPrmise(wx)

  //要领转换

  const getLocation = toPromiseWx('getLocation')

  const request = toPromiseWx('request')

  const setStorage = toPromiseWx('setStorage')

  //链式写逻辑

  getLocation() //获取位置信息

  .then(

  (res) => { //位置获取乐成后的处理,,res为返回信息

  //处理res后返回有用的信息,,这里直接返回res,,用于演示

  return Promise.resolve(res) //必需

  },

  (err) => { //位置获取失败后的过失处理,,err为过失信息

  //过失处理

  return Promise.resolve(err) //必需

  }

  )

  .then(

  (res) => { //凭证位置获取乐成后的信息,,请求天气信息

  return request({ url: 'http://api.weather.com'}) //返回一个pending 状态下的Promise

  }

  )

  .then(

  (res) => { //天气获取乐成后存入storage的回调

  setStorage({

  key: 'test',

  data: 'res'

  })

  },

  (err) => {

  //天气获取失败后执行这里,,err为获取天气失败的过失信息

  }

  )

  若是使用官方的API写上述逻辑,,代码是这样的:

  wx.getLocation({

  success: (res) => {

  //some transformation with res

  wx.request({

  url: 'http://api.weather.com',

  success: (res) => {

  wx.setStorage({

  success: () => {

  //do something

  },

  fail: (err) => {

  //do something if err happend

  }

  })

  },

  fail: (err) => {

  //do something if err happend

  }

  })

  },

  fail: (err) => {

  //do something if err happend

  })

  //层层回调,,若是逻辑再重大点,,可能就疯了



【本站声明】
  1、本站文章中所选用的图片及文字泉源于网络以及用户投稿,,由于未联系到知识产权人或未发明有关知识产权的挂号,,若有知识产权人并不肯意我们使用,,若是有侵权请连忙联系。。
  2、本网站差池文章中所涉及的内容真实性、准确性、可靠性认真,,仅系客观性形貌,,如您需要相识该类商品/服务详细的资讯,,请您直接与该类商品/服务的提供者联系。。


KESION pp电子软件

KESION pp电子软件是海内领先的在线教育软件及私域社交电商软件服务提供商,,恒久专注于为企业提供在线教育软件及社交电商SaaS平台解决方案。。
公司焦点产品云开店SaaS社交电商服务平台、在线教育SaaS服务平台、教育企业数字化SaaS云平台、企微营销助手、私有化自力安排品牌网校和在线教育咨询等。。

KESION 一直通过手艺立异,,提供产品和服务,,助力企业向数字化转型,,通过科技驱动商业刷新,,让商业变得更智慧!



▼点击进入pp电子官网相识更多



热门标签
微信小程序
上/下篇
  • React转微信小程序:从React类界说到Component挪用

  • 基于后端云微信小程序开发教程

换一换相关推荐
精选内容
热门精选
pp电子·模拟器(试玩游戏)官方网站 pp电子·模拟器(试玩游戏)官方网站 pp电子·模拟器(试玩游戏)官方网站
【网站地图】
把微信小程序异步API转为Promise,,简化异步编程