ES6 promise 对象封装
之前项目中一直有如下的写法
ycxUploadFile(opts).then(res=>{ console.log(res) }).catch(err=>{ util.showTips('检查报告上传失败'); })
这里opts是请求参数,这里使用了ES6的 Promise 对象封装的请求. then 代表 ycxUploadFile()该函数执行完毕之后且成功所执行的函数,而catch是执行失败之后执行的函数.
其中then执行的是ycxUploadFile函数执行成功返回的Promise对象
var utilRequest = function(data){ console.log(data); return new Promise(function(resolve,reject){ setTimeout(function(){ if(false){ resolve("返回得结果") }else{ reject("操作失败"); } },1000) console.log("good") }) } utilRequest("data").then(res=>{ console.log(res+"...1"); }).catch(err=>{ console.log(err) })
本文来自博客园踩坑狭,作者:韩若明瞳,转载请注明原文链接:https://www.cnblogs.com/han-guang-xue/p/12176930.html