普通api什么时候支持Promise风格
发布于 4 年前 作者 jie79 3959 次浏览 来自 分享

先记录下再完善吧

别骂我,我就是做个笔记

https://developers.weixin.qq.com/miniprogram/dev/extended/utils/api-promise.html

API Promise化
扩展微信小程序api支持promise

安装
npm install --save miniprogram-api-promise
使用
在小程序入口(app.js)调用一次promisifyAll,只需要调用一次。

示例:

import { promisifyAll, promisify } from ‘miniprogram-api-promise’;

const wxp = {}
// promisify all wx’s api
promisifyAll(wx, wxp)
console.log(wxp.getSystemInfoSync())
wxp.getSystemInfo().then(console.log)
wxp.showModal().then(wxp.openSetting())

// compatible usage
wxp.getSystemInfo({success(res) {console.log(res)}})

// promisify single api
promisify(wx.getSystemInfo)().then(console.log)

回到顶部