函数
const makePromise = (obj, method, objParam)=>{
return new Promise((resolved, rejected) => {
var success = (res) => {
resolved(res);
}
var fail = (res) => {
rejected(res);
}
var param = {}
if (typeof (objParam) === "object") {
param = Object.assign(param, objParam);
}
param.success = success;
param.fail = fail;
if (typeof (obj) === "object" && typeof (method) ==="string" && method !== "" &&typeof(obj[method]) === "function"){
obj[method](param);
}else{
rejected({msg:"check the param, obj is "+typeof(obj)+" and method is "+typeof(method)});
}
});
}
module.exports = {
makePromise: makePromise
}
使用
wx.downloadFile({url:url,success:function(res){},fail:function(res){}})
pack.makePromise(wx,"downloadFile",{
url:url
}).then((res)=>{
}).catch((res)=>{
});
var fm = wx.getFileSystemManager();
fm.access({path:path,success:function(res){},fail:function(res){}});
var fm = wx.getFileSystemManager();
pack.makePromise(fm,"access",{
path:path
}).then((res)=>{
}).catch((res)=>{
});