Coder Social home page Coder Social logo

youngjuning / wx-promise-pro Goto Github PK

View Code? Open in Web Editor NEW
835.0 22.0 131.0 485 KB

✨强大、优雅的微信小程序异步库🚀

Home Page: https://youngjuning.js.org/wx-promise-pro/

License: MIT License

JavaScript 100.00%
promise es6 miniapp async await wx-promise-pro wx-promise

wx-promise-pro's Introduction

wx-promise-pro

✨强大、优雅的微信小程序异步库🚀

npm npm Financial Contributors on Open Collective

NPM

优势

  • 方便集成:一处引用,处处使用
  • 把微信小程序所有异步 API promise 化并挂在到wx.pro 对象下
  • 支持 ES2018 finally 特性
  • 支持 TypeScript 开发

安装

你也可以直接把 dist 目录下的 wx-promise-pro.js 拷贝到项目里使用

$ npm i wx-promise-pro -S
# or
$ yarn add wx-promise-pro

初始化

import { promisifyAll, promisify } from 'wx-promise-pro'
// promisify all wx‘s api
promisifyAll()
// promisify single api
promisify(wx.getSystemInfo)().then(console.log)

示例代码

wx.pro.showLoading({
  title: '加载中',
  mask: true
})
wx.pro.request({
  url: 'https://cnodejs.org/api/v1/topics',
  data: {},
  method: 'GET',
  header: {'content-type': 'application/json'}
}).then(res => {
  console.log(res)
}).catch(err => {
  console.log(err)
}).finally(() => {
  wx.pro.hideLoading()
})

已知问题

  • createSignallanDebug 可能会引发报错,所以并没有挂载到 wx.pro 对象上。

wx-promise-pro's People

Contributors

allcontributors[bot] avatar kala888 avatar monkeywithacupcake avatar upupming avatar wss534857356 avatar youngjuning avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wx-promise-pro's Issues

使用懒加载lazyInitChart后无法添加点击事件

使用懒加载lazyInitChart后添加点击事件无效果
wx.pro.lazyInitChart(option, echarts, '#myCard', this).then(chart => {
chart.on('click', function(params) {
console.log(params)
})
return chart
})

并无报错,只是点击无效果,如果不使用懒加载,添加点击事件无问题

为什么报这个错误

thirdScriptError
Unexpected token {
SyntaxError: Unexpected token {

import { promisifyAll, promisify } from 'wx-promise-pro';
promisifyAll()
//index.js
//获取应用实例
const app = getApp()

Page({

个人觉得没有必要引入这个库

/**
 * 这里定义wx api 的各种promise 版本
 */
export function login() {
  return new Promise((resolve, reject) => {
    wx.login({ success: resolve, fail: reject });
  });
}

export function getUserInfo(withCredentials = false) {
  return new Promise((resolve, reject) => {
    wx.getUserInfo({ withCredentials: withCredentials, success: resolve, fail: reject });
  });
}

export function setStorage(key, value) {
  return new Promise((resolve, reject) => {
    wx.setStorage({ key: key, data: value, success: resolve, fail: reject });
  });
}

export function getStorage(key) {
  return new Promise((resolve, reject) => {
    wx.getStorage({ key: key, success: resolve, fail: reject });
  });
}

export function getLocation(type) {
  return new Promise((resolve, reject) => {
    wx.getLocation({ type: type, success: resolve, fail: reject });
  });
}

export function getSetting() {
  return new Promise((resolve, reject) => {
    wx.getSetting({ success: resolve, fail: reject });
  });
}

然后在需要用到的地方,引入
import { getUserInfo } from "../../utils/wechat";

然后直接happy的用上了,而且,还不至于增加多大的包大小。

getUserInfo(true).then((res) => {
          this.userInfo = res.userInfo;
        }).catch((err) => {
          console.log(err);
        });

用Typescript开发,代码提示功能

现在用Typescripte做项目,VScode总是提示:类型“WX”上不存在属性“pro。
期待能出一个VSCode的代码提示文件。

另外项目中使用npm方式引入的库,然后使用如下 :
import 'wx-promise-pro';
onLoad(){
wx.pro.request({
url: 'https://cnodejs.org/api/v1/topics',
data: {},
method: 'GET',
header: {'content-type': 'application/json'}
}).then(res => {
console.log(res)
}).catch(err => {
console.log(err)
}).finally(() => {
wx.hideLoading({});
})
}

可是运行起来报如下错误:
VM4219:1 thirdScriptError
sdk uncaught third Error
module "pages/index/wx-promise-pro" is not defined
Error: module "pages/index/wx-promise-pro" is not defined
at require (http://127.0.0.1:18812/appservice/__dev__/WAService.js:1:716408)
at http://127.0.0.1:18812/appservice/__dev__/WAService.js:1:716158
at http://127.0.0.1:18812/appservice/pages/index/index.js:4:1
at require (http://127.0.0.1:18812/appservice/__dev__/WAService.js:1:716549)
at :8:7
at HTMLScriptElement.scriptLoaded (

请指点一下,谢谢

这种可行不

image

低侵入性。就是不晓得Proxy支持怎么样。目前我没有遇到问题

使用懒加载 多张图表 tooltip不显示,原来可以显示

使用懒加载 多张图表 tooltip不显示,原来可以显示

for(let i=0;i<arrays.length;i++){

 getOption(arrays[i].xArray, arrays[i].yArray).then(option => {
    console.log(option);
    wx.pro.lazyInitChart(option, echarts, '#mychart-bar'+i, this).then((chart) => {
      return chart
    })
  })
}`

Error: APP-SERVICE-SDK:createSignal:fail rejected due to no permission currently

今天发布后出现错误日志如下:

APP-SERVICE-SDK:createSignal:fail rejected due to no permission currently
Error: APP-SERVICE-SDK:createSignal:fail rejected due to no permission currently
at Object.createSignal (https://lib/WASubContext.js:2:1826102)
at https://miniprogram_npm/wx-promise-pro/index.js:8:3923
at c (https://miniprogram_npm/wx-promise-pro/index.js:8:3857)
at ve.onLaunch (https://app.js:148:7)
at ve.<anonymous> (https://lib/WASubContext.js:2:2124238)
at new ve (https://lib/WASubContext.js:2:2124994)
at Function.<anonymous> (https://lib/WASubContext.js:2:2125306)
at https://lib/WASubContext.js:2:2111785
at https://app.js:48:1

使用Source Map 定位发现错误代码位置是:

n=function(){wx.pro={},Object.keys(wx).forEach((function(e){a.indexOf(e)>=0?wx.pro[e]=r(wx[e]):wx.pro[e]=wx[e]}))}

感觉是赋值 wx.pro[createSignal]=wx[createSignal] 出现的问题

仿照demo使用,提示this.asyncFunc(...).then(...).catch(...).finally is not a function

**this.asyncFunc(...).then(...).catch(...).finally is not a function;**at "pages/iview-test/iview-test" page lifeCycleMethod onLoad function
TypeError: this.asyncFunc(...).then(...).catch(...).finally is not a function
at r.onLoad (http://127.0.0.1:63760/appservice/pages/iview-test/iview-test.js:235:15)
at r. (http://127.0.0.1:63760/appservice/__dev__/WAService.js:2:4625)
at q (http://127.0.0.1:63760/appservice/__dev__/WAService.js:1:9110)
at Z (http://127.0.0.1:63760/appservice/__dev__/WAService.js:1:11143)
at te (http://127.0.0.1:63760/appservice/__dev__/WAService.js:1:11413)
at Function. (http://127.0.0.1:63760/appservice/__dev__/WAService.js:1:14157)
at http://127.0.0.1:63760/appservice/__dev__/WAService.js:1:19990
at http://127.0.0.1:63760/appservice/__dev__/WAService.js:8:19668
at Array.forEach ()
at Function. (http://127.0.0.1:63760/appservice/__dev__/WAService.js:8:19648)
console.error @ VM23502:1
errorReport @ WAService.js:5
thirdErrorReport @ WAService.js:5
(anonymous) @ WAService.js:5
(anonymous) @ WAService.js:2
q @ WAService.js:1
Z @ WAService.js:1
te @ WAService.js:1
(anonymous) @ WAService.js:1
(anonymous) @ WAService.js:1
(anonymous) @ WAService.js:8
(anonymous) @ WAService.js:8
(anonymous) @ WAService.js:6
a @ appservice?t=1529636407442:7941
d.registerCallback @ appservice?t=1529636407442:7941
m.forEach @ appservice?t=1529636407442:7941
d @ appservice?t=1529636407442:7941
j.onmessage @ appservice?t=1529636407442:7941

import regeneratorRuntime from '../../utils/wxPromise.js'
import '../../utils/wxPromise.js'

asyncFunc: async function (username) {
    try {
      let result;
      await this.awaitFunc().then(res => {
        console.log(res);
        result = res;
      })
      return result;
    } catch (e) {
      throw e
    }
  },
  awaitFunc() {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve([1, 2, 3])
      }, 500)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.asyncFunc('youngjuning').then((res) => {
        console.log(res)
      })
      .catch((err) => {
        console.log(err)
      })
    .finally(() => {
      // wx.hideLoading()
      console.log("finally");
    })
  },

asycn方法的finally会报错

this.syncLocation(this).then(res=>{
console.log('<-------->'+res);
})
.catch(err=>{
console.log(err);
})
//这里加上去会报错。
.finally(() => {
wx.hideLoading()
})

//这里是方法定义
syncLocation: async (ops) => {
let that = ops;
// let loc = new Location();
try{
let data = await Location.getCurrentLocation();

console.log("========72=====" + Location.jd);
that.setData({ location: data });
return data;
} catch (e) {
  throw e
};

wx.canvasToTempFilePath有问题

image
image
这有问题吧,wx.canvasToTempFilePath方法本身不需要canvasContext对象,这样的话,我连保存的的类型为JPG都不行了,下个版本什么时候出。期待...

没有自动补全?

用 wx.pro 的话,好像代码提示和自动补全功能,都会打折,用起来很难受啊

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.