网站首页 > 博客文章 正文
promise 相当于一个规范 解决js中多个一步回调优化/维护...的问题。
原生中提供了promise对象,es6中promise对象是一个构造函数,用来生成promise实例。
const promise=new Promise(function(resolve,reject){ resolve(1); }) 接受一个函数作为参数,二个参数 由js引擎提供,不用自己部署 resolve 的作用是promise对象的状态从未完成到成功,成功后,将异步操作的结果作为参数传递; reject 的作用是promise对象的状态从未完成到失败,失败后,将异步操作的结果作为参数传递;
then()方法分别制定resolve reject状态的回调函数
jquery defrred 就是jquery中promise的实现
const getJSON=function(url,type,data){ const promise=new Promise(function(resolve,reject){ let xml=new XMLHttpRequest(); xml.open(type,url); if(type=='get'){ xml.send(); }else{ xml.setRequestHeader('Content-type','applicateion/json'); xml.send(JSON.stringify(data)); } const handler=function(){ if(xml.readyState==4 && xml.status==200){ resolve(); }else{ reject(); } xml.onreadystatechange=handler; } }) } let list= (name,msg) =>{ getJSON('http://localhost:3000/add','post',{name:name,msg:msg}) .then(function(res){ //成功后 支持延续任务的调用,可以写多个then console.log(res); },function(error){ //失败后 console.log(error); }) }
预请求:就是先测试性请求(options)
//jquery 低于1.5版本返回的是XHR对象,高于defrred对象 $.ajax({url:'',type;'get',dataType:'json'}) .done(function(res){ //done不支持延续任务的调用 console.log(res) }) .fail(function(error){ console.log(error) })
猜你喜欢
- 2024-10-27 前端面试大全:手写 Promise(前端手机端面试题)
- 2024-10-27 Javascript ES6中 Generator的?async/await Promise 了解一下?
- 2024-10-27 Promise 中 race 方法的目的是什么
- 2024-10-27 ES6 Promise对象(es6对象操作)
- 2024-10-27 ES6 Promise 的最佳实践(es6 promise作用)
- 2024-10-27 ES6学习(17):彻底搞懂 async 和 await,轻松掌握异步编程!
- 2024-10-27 自己造轮子,超详细、简单的Promise对象原理讲解及代码实现
- 2024-10-27 JavaScript ES6 语法特性介绍(javascript的基本语法遵循的标准是____。)
- 2024-10-27 图解 Promise 实现原理(一):基础实现
- 2024-10-27 可视化的 js:动态图演示 Promises & Async/Await 的过程
你 发表评论:
欢迎- 07-07Xiaomi Enters SUV Market with YU7 Launch, Targeting Tesla with Bold Pricing and High-Tech Features
- 07-07Black Sesame Maps Expansion Into Robotics With New Edge AI Strategy
- 07-07Wuhan's 'Black Tech' Powers China's Cross-Border Push with Niche Electronics and Scientific Firepower
- 07-07Maven 干货 全篇共:28232 字。预计阅读时间:110 分钟。建议收藏!
- 07-07IT运维必会的30个工具(it运维工具软件)
- 07-07开源项目有你需要的吗?(开源项目什么意思)
- 07-07自动化测试早就跑起来了,为什么测试管理还像在走路?
- 07-07Cursor 最强竞争对手来了,专治复杂大项目,免费一个月
- 最近发表
-
- Xiaomi Enters SUV Market with YU7 Launch, Targeting Tesla with Bold Pricing and High-Tech Features
- Black Sesame Maps Expansion Into Robotics With New Edge AI Strategy
- Wuhan's 'Black Tech' Powers China's Cross-Border Push with Niche Electronics and Scientific Firepower
- Maven 干货 全篇共:28232 字。预计阅读时间:110 分钟。建议收藏!
- IT运维必会的30个工具(it运维工具软件)
- 开源项目有你需要的吗?(开源项目什么意思)
- 自动化测试早就跑起来了,为什么测试管理还像在走路?
- Cursor 最强竞争对手来了,专治复杂大项目,免费一个月
- Cursor 太贵?这套「Cline+OpenRouter+Deepseek+Trae」组合拳更香
- 为什么没人真的用好RAG,坑都在哪里? 谈谈RAG技术架构的演进方向
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)