const bizContent={ 'method.name':{ }, 'method2.name2':{ },};//控制器const controller={ init(bizContent){ const keyArr=[] const methodArr=[] for(let key in bizContent){ const arr=/(\w+)\.(\w+)/.exec(key) let method=arr[1]; if(method&&this[method]){ keyArr.push(key); methodArr.push(this[method](bizContent[key],arr[1],arr[2])) } } const resData={} return Promise.all(methodArr).then((resArr)=>{ for(let i=0;i<resArr.length;i++){ const key=keyArr[i]; const data=resArr[i]; resData[key]=data } return resData; }) }, method:async function (option,method,name) { return 21 }, method2:async function (option,method,name) { throw 21 }}controller.init(bizContent).then(function (d) { console.log(d)}).catch(function (err) { return { flag:'F', msg:err, }})
来源:https://www.cnblogs.com/caoke/p/11827178.html