promise学习
来源:互联网 发布:淘宝互刷平台排行榜 编辑:程序博客网 时间:2024/05/22 00:29
1.将传统的ajax调用改写为promise的方式
function get(url) { // Return a new promise. return new Promise(function(resolve, reject) { // Do the usual XHR stuff var req = new XMLHttpRequest(); req.open('GET', url); req.onload = function() { // This is called even on 404 etc // so check the status if (req.status == 200) { // Resolve the promise with the response text resolve(req.response); } else { // Otherwise reject with the status text // which will hopefully be a meaningful error reject(Error(req.statusText)); } }; // Handle network errors req.onerror = function() { reject(Error("Network Error")); }; // Make the request req.send(); });}
get('story.json').then(function(response) { console.log("Success!", response);}, function(error) { console.error("Failed!", error);})
2.当从 then()
回调中返回某些内容时,如果返回一个值,则会以该值调用下一个 then()
。但是,如果返回类promise 的内容,下一个then()
则会等待,并仅在 promise 产生结果(成功/失败)时调用。阅读全文
0 0
- 学习Promise
- promise学习
- promise学习
- js Promise学习
- promise对象学习
- 学习 ES6 Promise
- es6 标准 Promise 学习笔记
- Promise学习记录(一)
- 学习promise编写和使用
- Promise/commonJS/AMD学习笔记
- 学习 Promise (结合 Rxjava)
- [个人学习]另一个 Promise 对象
- ES6 Promise对象学习总结
- ES6学习11(Promise)
- es6学习笔记之Promise
- Promise学习--继续更新
- Promise学习笔记(一)
- 学习promise编写和使用
- 在Windows中使用VirtualBox安装Ubuntu
- qt编译时报 无法执行“rc.exe”
- 归并排序和几种内部排序的比较
- 2017前端面试题大全
- UITableView->estimatedRowHeight-> 解决加载缓慢 多次调用cellForRowAt
- promise学习
- SSH综合项目实战(快递) -- day06 基于CXF发布CRM服务、定区关联客户
- sql中的in与not in,exists与not exists的区别
- git在当前分支下,回滚文件到任何commit
- js设计模式与开发实践1
- 关于输出超限的解决方法
- Too many connections 解决思路
- POJ1426-Find The Multiple(搜索)
- 数据结构-查找算法总结