oak-frontend-base/es/utils/promisify.js

22 lines
634 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 将小程序的API封装成支持Promise的API, 小程序部分api不支持promise
* @params fn {Function} 小程序原始API如wx.login
*/
export function promisify(fn) {
return function (obj) {
return new Promise((resolve, reject) => {
obj.success = function (res) {
resolve(res);
};
obj.fail = function (res) {
reject(res);
};
obj.cancel = function () {
// 微信jsSdk api有cancel回调
reject({ errMsg: 'request:cancel' });
};
fn(obj);
});
};
}