前端开发 Vue -4promise解读1
JS(JavaScript) - Promise
2015年6月, ES2015(即 ECMAScript 6、ES6) 正式发布。其中 Promise 被列为正式规范,成为 ES6 中最重要的特性之一。
Promises 的概念是由 CommonJS 小组的成员在 Promises/A 规范中提出来的。
https://www.hangge.com/blog/cache/detail_1635.html
https://www.hangge.com/blog/cache/detail_1638.html
一、promises相关概念
1,then()方法介绍
- 成功回调
- 失败回调
- 前进回调(规范没有要求包括前进回调的实现,但是很多都实现了)。
2,Promise对象状态
- Pending(进行中、未完成的)
- Resolved(已完成,又称 Fulfilled)
- Rejected(已失败)。
3,Promise/A规范图解

4,目前支持Promises/A规范的库
- Q:可以在NodeJS 以及浏览器上工作,与jQuery兼容,可以通过消息传递远程对象。
- RSVP.js:一个轻量级的库,它提供了组织异步代码的工具。
- when.js:体积小巧,使用方便。
- NodeJS的Promise
- jQuery 1.5:据说是基于“CommonJS Promises/A”规范
- WinJS / Windows 8 / Metro
二、使用promises的优势
1,解决回调地狱(Callback Hell)问题
1
2
3
4
5
6
7
8
9
10
11
12
|
firstAsync( function (data){ //处理得到的 data 数据 //.... secondAsync( function (data2){ //处理得到的 data2 数据 //.... thirdAsync( function (data3){ //处理得到的 data3 数据 //.... }); }); }); |
(2)如果使用 promises 的话,代码就会变得扁平且更可读了。前面提到 then 返回了一个 promise,因此我们可以将 then 的调用不停地串连起来。其中 then 返回的 promise 装载了由调用返回的值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
firstAsync() .then( function (data){ //处理得到的 data 数据 //.... return secondAsync(); }) .then( function (data2){ //处理得到的 data2 数据 //.... return thirdAsync(); }) .then( function (data3){ //处理得到的 data3 数据 //.... }); |
2,更好地进行错误捕获
(1)比如下面代码我们使用 setTimeout 模拟异步操作,在其中抛出了个异常。但由于异步回调中,回调函数的执行栈与原函数分离开,导致外部无法抓住异常。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
function fetch(callback) { setTimeout(() => { throw Error( '请求失败' ) }, 2000) } try { fetch(() => { console.log( '请求处理' ) // 永远不会执行 }) } catch (error) { console.log( '触发异常' , error) // 永远不会执行 } // 程序崩溃 // Uncaught Error: 请求失败 |
(2)如果使用 promises 的话,通过 reject 方法把 Promise 的状态置为 rejected,这样我们在 then 中就能捕捉到,然后执行“失败”情况的回调。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
function fetch(callback) { return new Promise((resolve, reject) => { setTimeout(() => { reject( '请求失败' ); }, 2000) }) } fetch() .then( function (data){ console.log( '请求处理' ); console.log(data); }, function (reason, data){ console.log( '触发异常' ); console.log(reason); } ); |
当然我们在 catch 方法中处理 reject 回调也是可以的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
function fetch(callback) { return new Promise((resolve, reject) => { setTimeout(() => { reject( '请求失败' ); }, 2000) }) } fetch() .then( function (data){ console.log( '请求处理' ); console.log(data); } ) . catch ( function (reason){ console.log( '触发异常' ); console.log(reason); }); |
原文出自:www.hangge.com 转载请保留原文链接:https://www.hangge.com/blog/cache/detail_1635.html
1,then()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
//做饭 function cook(){ console.log( '开始做饭。' ); var p = new Promise( function (resolve, reject){ //做一些异步操作 setTimeout( function (){ console.log( '做饭完毕!' ); resolve( '鸡蛋炒饭' ); }, 1000); }); return p; } //吃饭 function eat(data){ console.log( '开始吃饭:' + data); var p = new Promise( function (resolve, reject){ //做一些异步操作 setTimeout( function (){ console.log( '吃饭完毕!' ); resolve( '一块碗和一双筷子' ); }, 2000); }); return p; } function wash(data){ console.log( '开始洗碗:' + data); var p = new Promise( function (resolve, reject){ //做一些异步操作 setTimeout( function (){ console.log( '洗碗完毕!' ); resolve( '干净的碗筷' ); }, 2000); }); return p; } |
(2)使用 then 链式调用这三个方法:
1
2
3
4
5
6
7
8
9
10
|
cook() .then( function (data){ return eat(data); }) .then( function (data){ return wash(data); }) .then( function (data){ console.log(data); }); |
当然上面代码还可以简化成如下:
1
2
3
4
5
6
|
cook() .then(eat) .then(wash) .then( function (data){ console.log(data); }); |
(3)运行结果如下:

2,reject()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
//做饭 function cook(){ console.log( '开始做饭。' ); var p = new Promise( function (resolve, reject){ //做一些异步操作 setTimeout( function (){ console.log( '做饭失败!' ); reject( '烧焦的米饭' ); }, 1000); }); return p; } //吃饭 function eat(data){ console.log( '开始吃饭:' + data); var p = new Promise( function (resolve, reject){ //做一些异步操作 setTimeout( function (){ console.log( '吃饭完毕!' ); resolve( '一块碗和一双筷子' ); }, 2000); }); return p; } cook() .then(eat, function (data){ console.log(data + '没法吃!' ); }) |
运行结果如下:

1
2
3
4
|
cook() .then( null , function (data){ console.log(data + '没法吃!' ); }) |
3,catch()方法
(1)它可以和 then 的第二个参数一样,用来指定 reject 的回调
1
2
3
4
5
|
cook() .then(eat) . catch ( function (data){ console.log(data + '没法吃!' ); }); |
(2)它的另一个作用是,当执行 resolve 的回调(也就是上面 then 中的第一个参数)时,如果抛出异常了(代码出错了),那么也不会报错卡死 js,而是会进到这个 catch 方法中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
//做饭 function cook(){ console.log( '开始做饭。' ); var p = new Promise( function (resolve, reject){ //做一些异步操作 setTimeout( function (){ console.log( '做饭完毕!' ); resolve( '鸡蛋炒饭' ); }, 1000); }); return p; } //吃饭 function eat(data){ console.log( '开始吃饭:' + data); var p = new Promise( function (resolve, reject){ //做一些异步操作 setTimeout( function (){ console.log( '吃饭完毕!' ); resolve( '一块碗和一双筷子' ); }, 2000); }); return p; } cook() .then( function (data){ throw new Error( '米饭被打翻了!' ); eat(data); }) . catch ( function (data){ console.log(data); }); |
运行结果如下:

1
2
3
4
5
6
7
8
9
10
11
|
somePromise.then( function () { return a(); }). catch (TypeError, function (e) { //If a is defined, will end up here because //it is a type error to reference property of undefined }). catch (ReferenceError, function (e) { //Will end up here if a wasn't defined at all }). catch ( function (e) { //Generic catch-the rest, error wasn't TypeError nor //ReferenceError }); |
4,all()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
//切菜 function cutUp(){ console.log( '开始切菜。' ); var p = new Promise( function (resolve, reject){ //做一些异步操作 setTimeout( function (){ console.log( '切菜完毕!' ); resolve( '切好的菜' ); }, 1000); }); return p; } //烧水 function boil(){ console.log( '开始烧水。' ); var p = new Promise( function (resolve, reject){ //做一些异步操作 setTimeout( function (){ console.log( '烧水完毕!' ); resolve( '烧好的水' ); }, 1000); }); return p; } Promise .all([cutUp(), boil()]) .then( function (results){ console.log( "准备工作完毕:" ); console.log(results); }); |
(2)运行结果如下:

5,race()方法
1
2
3
4
5
6
|
Promise .race([cutUp(), boil()]) .then( function (results){ console.log( "准备工作完毕:" ); console.log(results); }); |

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
//请求某个图片资源 function requestImg(){ var p = new Promise( function (resolve, reject){ var img = new Image(); img.onload = function (){ resolve(img); } img.src = 'xxxxxx' ; }); return p; } //延时函数,用于给请求计时 function timeout(){ var p = new Promise( function (resolve, reject){ setTimeout( function (){ reject( '图片请求超时' ); }, 5000); }); return p; } Promise .race([requestImg(), timeout()]) .then( function (results){ console.log(results); }) . catch ( function (reason){ console.log(reason); }); |
上面代码 requestImg 函数异步请求一张图片,timeout 函数是一个延时 5 秒的异步操作。我们将它们一起放在 race 中赛跑。
- 如果 5 秒内图片请求成功那么便进入 then 方法,执行正常的流程。
- 如果 5 秒钟图片还未成功返回,那么则进入 catch,报“图片请求超时”的信息。

原文出自:www.hangge.com 转载请保留原文链接:https://www.hangge.com/blog/cache/detail_1638.html
前端开发 Vue -4promise解读1的更多相关文章
- 前端开发 Vue -4promise解读2
https://www.runoob.com/vue2/vue-tutorial.html promise promise是什么? 1.主要用于异步计算 2.可以将异步操作队列化,按照期望的顺序执 ...
- 前端开发 vue,angular,react框架对比2
在过去一年里,前端开发发展迅速,前端工程师的薪资亦是水涨船高.2019 更是热度不减,而作为近年来尤为热门的前端框架,Vue.js 自是积累了大量关注.那么,Vue.js 是适合你的框架吗? ...
- 前端开发 vue,angular,react框架对比1
转载自:https://www.cnblogs.com/hubgit/p/6633214.html 首先,我们先了解什么是MVX框架模式? MVX框架模式:MVC+MVP+MVVM 1.MVC:Mod ...
- 前端开发 Vue Vue.js和Nodejs的关系
首先vue.js 是库,不是框架,不是框架,不是框架. Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据. Vue.js 的核心是一个允许你 ...
- 前端开发 Vue -1windows环境搭建Vue Node开发环境
解决几个疑问: 想学习下vue.js,我理解的它是一个前端的框架,主要作用是对数据的处理,和juqery类似,所以不太理解为什么要在nodejs中npm install vue呢?在html文件中引入 ...
- 前端开发 Vue -0前言
Vue2.0 新手完全填坑攻略——从环境搭建到发布 Vue2 入门,读这篇就够了 Jinkey原创感谢 showonne.yubang 技术指导Demo 地址:http://demo.jinkey.i ...
- 前端开发 Vue -3axios
Axios是什么? 应该念“阿克希奥斯”……但是太长太拗口,我一般念“阿笑斯”…… Axios 是一个基于 promise 的 HTTP 库,简单的讲就是可以发送get.post请求.说到get.po ...
- 前端开发--vue开发部分报错指南
前期开发过程中 [Vue warn]: Error in render: "TypeError: Cannot read property '0' of undefined". 解 ...
- [前端开发]Vue父子组件的通信及访问
父传子 props 子传父 自定义事件emit props传数组 props:['cmovies','cmessage'] props传对象 props:{ //1.类型限制 cmovies:Arra ...
随机推荐
- return语句——学习笔记
return,可以提前结束其所在函数. 函数内不写,会自动加上return. 非引用返回: 引用返回:a=3,b=3 注意事项: 两种修改字符串某一位置值的方式:
- ISO/IEC 9899:2011 条款6.5.5——乘法操作符
6.5.5 乘法操作符 语法 1.multiplicative-expression: cast-expression multiplicative-expression * cast-e ...
- opencv图片旋转90度
#include<iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv ...
- CentOS7下使用Sentinel实现Redis集群高可用
Sentinel是Redis官方提供的一种高可用方案(除了Sentinel,Redis Cluster是另一种方案),它可以自动监控Redis master/slave的运行状态,如果发现master ...
- Day1作业2:多层菜单查询
流程图: code: #!/usr/bin/env python # encoding: utf-8 # Auther:ccorz Mail:ccniubi@163.com Blog:http://w ...
- WINDOWS配置WSUS。
wsus的注册表文件! Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINESOFTWAREPoliciesMicrosoftWindows ...
- HTML布局水平导航条1制作
该文是用css制作个导航条,用竖线分隔,导航条是点击的多个区块.步骤:ul里设置需要数量的li,li中加上a链接给ul加样式,去掉默认的前面的点给li设置左浮动,让ul里的li横向排列a链接设置成块状 ...
- java四大名著及idea常用插件
四大名著 <Java编程思想><Think in Java> https://github.com/quanke/think-in-java https://github.co ...
- 简简单单储存过程——循环一个select结果集
原文地址:https://shitou521.iteye.com/blog/1069027 摘要:本文主要讲解了存储过程的创建.调用.以及游标的使用 ,相信掌握了游标 会对你有不错的帮助,有 ...
- 个人博客搭建全记录(Hexo,Github)
搭建过程主要借鉴小歪的博客 博客主题airclod Hexo,Github建站记录 1. 准备 Github账号 注册登陆Github 创建Repository,Repository Name就是Yo ...