复习

time 6m8s
then里面两个方法,选择其中一个回调方法运行,因为a没有定义产生错误所以走reject
即使是 resolve(a)也不会走onfulfilled,而会走onrejected,这是两种状态
image.png

  1. let promise = new Promise((resolve, reject) => {
  2. resolve(a);
  3. })
  4. promise.then((value) => {
  5. console.log('resolve:' + value);
  6. }, (reason) => {
  7. console.log('reject:' + reason);
  8. })
  9. /*reject:ReferenceError: a is not defined*/

time 7m08
只要在函数中抛出错误,一定会更改promise的状态,调用onrejected函数

  1. let promise = new Promise((resolve, reject) => {
  2. // resolve(a);
  3. console.log(a)
  4. })
  5. promise.then((value) => {
  6. console.log('resolve:' + value);
  7. }, (reason) => {
  8. console.log('reject:' + reason);
  9. })
  10. /*reject:ReferenceError: a is not defined*/

如果没有

  1. let promise = new Promise((resolve, reject) => {
  2. // resolve(a);
  3. // console.log(a)
  4. console.log(1);
  5. return 1;
  6. })
  7. promise.then((value) => {
  8. console.log('resolve:' + value);
  9. }, (reason) => {
  10. console.log('reject:' + reason);
  11. })
  12. /*1*/

只想写失败回调时,用null占位成功回调

time 8m37s
只想写失败回调的时候,把成功回调设置为null

  1. let promise = new Promise((resolve, reject) => {
  2. resolve(a);
  3. })
  4. /*promise.then((value) => {
  5. console.log('resolve:' + value);
  6. }, (reason) => {
  7. console.log('reject:' + reason);
  8. })*/
  9. promise.then(null, (reason) => {
  10. console.log('reject:' + reason);
  11. })

promise.catch

time 9m23s

  1. let promise = new Promise((resolve, reject) => {
  2. resolve(a);
  3. })
  4. /*promise.then((value) => {
  5. console.log('resolve:' + value);
  6. }, (reason) => {
  7. console.log('reject:' + reason);
  8. })*/
  9. /*
  10. promise.then(null, (reason) => {
  11. console.log('reject:' + reason);
  12. })*/
  13. promise.catch(function (reason) {
  14. console.log('reject:' + reason);
  15. })

写法比较

两种写法推荐第二种

  1. let promise = new Promise((resolve, reject) => {
  2. // resolve(a);
  3. // console.log(a)
  4. console.log(1);
  5. })
  6. promise.then(function () {
  7. }, function () {
  8. })
  9. promise
  10. .then(function () {
  11. })
  12. .catch(function () {
  13. })

特征:固化

time 11m57s
当resolve时,即使之后代码出错,讲道理应该走catch了,但因为固化不会走
特性固化:状态被固定,不会在同一处再被改变了

  1. let promise = new Promise((resolve, reject) => {
  2. /*依然是ok,因为resolve先执行,不会走catch,但下面的代码依然运行,只不过不报错*/
  3. resolve('ok');
  4. console.log(1)
  5. console.log(a)
  6. })
  7. promise
  8. .then(function (value) {
  9. console.log(value);
  10. })
  11. .catch(function (err) {
  12. console.log(err);
  13. })
  14. /*1
  15. ok*/

多个then,链式调用

time 17m02s
image.png
then()如果什么参数都不传, 会直接忽略;

固化理解

time 20m19s

多个promise嵌套是如何传递的

time 22m
time 29m25s

  1. const p1 = new Promise((resolve, reject) => {
  2. setTimeout(function () {
  3. reject(new Error('fail'));
  4. }, 3000)
  5. })
  6. const p2 = new Promise((resolve, reject) => {
  7. setTimeout(function () {
  8. resolve(p1);
  9. }, 1000)
  10. })

image.png
解析

  1. /*等reject(new Error('fail'));执行完,p1才有值*/
  2. const p1 = new Promise((resolve, reject) => {
  3. setTimeout(function () {
  4. reject(new Error('fail'));
  5. }, 3000)
  6. })
  7. const p2 = new Promise((resolve, reject) => {
  8. setTimeout(function () {
  9. /*因为调用p2调用p1,需要等到promise执行完,才能得到p1的值
  10. * p1运行完,固化了,p2被p1影响*/
  11. resolve(p1);
  12. }, 1000)
  13. })

time 31m14s
状态依赖

  1. /*等reject(new Error('fail'));执行完,p1才有值*/
  2. const p1 = new Promise((resolve, reject) => {
  3. setTimeout(function () {
  4. reject(new Error('fail'));
  5. /*Error: fail
  6. at app.js:139:16*/
  7. }, 3000)
  8. })
  9. const p2 = new Promise((resolve, reject) => {
  10. setTimeout(function () {
  11. /*因为调用p2调用p1,需要等到promise执行完,才能得到p1的值
  12. * p1运行完,固化了,p2被p1影响*/
  13. resolve(p1);
  14. }, 1000)
  15. })
  16. p2.then(result=>console.log(result))
  17. .catch(err=>console.log(err))

image.png

all

time 41m24s

time 48m16s
用promise包裹异步操作

  1. const fs = require('fs');
  2. let promise1 = new Promise((resolve, reject) => {
  3. fs.readFile('./name.txt', 'utf-8', function (err, data) {
  4. console.log(1);//1
  5. if (err){
  6. console.log(err);
  7. reject(err);
  8. }
  9. console.log(data);//hhjkahfkhakh
  10. resolve(data);
  11. })
  12. })

必须成功才能作为返回值

注意:
1error参数必须得带着
2

  1. const fs = require('fs');
  2. let promise1 = new Promise((resolve, reject) => {
  3. fs.readFile('./name.txt', 'utf-8', function (err, data) {
  4. // console.log(1);//1
  5. if (err){
  6. console.log(err);
  7. reject(err);
  8. }
  9. console.log(data);
  10. resolve(data);
  11. })
  12. })
  13. let promise2 = new Promise((resolve, reject) => {
  14. fs.readFile('./number.txt', 'utf-8', function (err, data) {
  15. // console.log(1);//1
  16. if (err){
  17. console.log(err);
  18. reject(err);
  19. }
  20. console.log(data);
  21. resolve(data);
  22. })
  23. })
  24. let promise3 = new Promise((resolve, reject) => {
  25. fs.readFile('./score.txt', 'utf-8', function (err, data) {
  26. // console.log(1);//1
  27. if (err){
  28. console.log(err);
  29. reject(err);
  30. }
  31. console.log(data);
  32. resolve(data);
  33. })
  34. })
  35. const p= Promise.all([promise1,promise2,promise3]);
  36. p.then(res=>console.log(res));
  37. console.log(p);

image.png

读写失败

time 56m11s

  1. const fs = require('fs');
  2. let promise1 = new Promise((resolve, reject) => {
  3. fs.readFile('./name.txt', 'utf-8', function (err, data) {
  4. // console.log(1);//1
  5. /* if (err){
  6. console.log(err);
  7. reject(err);
  8. }
  9. console.log(data);*/
  10. resolve(data);
  11. })
  12. })
  13. let promise2 = new Promise((resolve, reject) => {
  14. fs.readFile('./number.txt', 'utf-8', function (err, data) {
  15. // console.log(1);//1
  16. /* if (err){
  17. console.log(err);
  18. reject(err);
  19. }
  20. console.log(data);*/
  21. resolve(data);
  22. })
  23. })
  24. /*'./score.tx'读取失败,返回undefined*/
  25. let promise3 = new Promise((resolve, reject) => {
  26. fs.readFile('./score.tx', 'utf-8', function (err, data) {
  27. // console.log(1);//1
  28. /* if (err){
  29. console.log(err);
  30. reject(err);
  31. }
  32. console.log(data);*/
  33. resolve(data);
  34. })
  35. })
  36. const p= Promise.all([promise1,promise2,promise3]);
  37. p.then(res=>console.log(res));
  38. // console.log(p);
  39. /*[ './name.txt', './number.txt', undefined ]*/

.catch抛出错误

time 58m

  1. const fs = require('fs');
  2. let promise1 = new Promise((resolve, reject) => {
  3. fs.readFile('./name.txt', 'utf-8', function (err, data) {
  4. // console.log(1);//1
  5. /* if (err){
  6. console.log(err);
  7. reject(err);
  8. }
  9. console.log(data);*/
  10. resolve(data);
  11. })
  12. })
  13. let promise2 = new Promise((resolve, reject) => {
  14. fs.readFile('./number.txt', 'utf-8', function (err, data) {
  15. // console.log(1);//1
  16. /* if (err){
  17. console.log(err);
  18. reject(err);
  19. }
  20. console.log(data);*/
  21. resolve(data);
  22. })
  23. })
  24. /*'./score.tx'读取失败,返回undefined*/
  25. let promise3 = new Promise((resolve, reject) => {
  26. fs.readFile('./score.tx', 'utf-8', function (err, data) {
  27. // console.log(1);//1
  28. if (err){
  29. // console.log(err);
  30. reject(err);
  31. }
  32. // console.log(data);
  33. resolve(data);
  34. })
  35. })
  36. const p= Promise.all([promise1,promise2,promise3]);
  37. p.then(res=>console.log(res))
  38. .catch(err=>console.log(err));
  39. // console.log(p);
  40. /*[ './name.txt', './number.txt', undefined ]*/

image.png

time 1h1m6s

  1. let promise1 = new Promise((resolve, reject) => {
  2. setTimeout(function () {
  3. resolve('promise1: 1000ms');
  4. }, 1000)
  5. })
  6. let promise2 = new Promise((resolve, reject) => {
  7. setTimeout(function () {
  8. resolve('promise2: 2000ms');
  9. }, 2000)
  10. })
  11. let promise3 = new Promise((resolve, reject) => {
  12. setTimeout(function () {
  13. resolve('promise3: 3000ms');
  14. }, 3000)
  15. })
  16. let p = Promise.all([promise1, promise2, promise3]);
  17. p.then(res => console.log( res))
  18. .catch(err => console.log('reject:' + err));
  19. /*[ 'promise1: 1000ms', 'promise2: 2000ms', 'promise3: 3000ms' ]*/

全部变成reject
tme 1h4m
如果失败只打印第一个失败的内容,即使前面的成功了也不会打印,后面的失败也不会打印

  1. let promise1 = new Promise((resolve, reject) => {
  2. setTimeout(function () {
  3. reject('promise1: 1000ms');
  4. }, 1000)
  5. })
  6. let promise2 = new Promise((resolve, reject) => {
  7. setTimeout(function () {
  8. reject('promise2: 2000ms');
  9. }, 2000)
  10. })
  11. let promise3 = new Promise((resolve, reject) => {
  12. setTimeout(function () {
  13. reject('promise3: 3000ms');
  14. }, 3000)
  15. })
  16. let p = Promise.all([promise1, promise2, promise3]);
  17. p.then(res => console.log( res))
  18. .catch(err => console.log('reject:' + err));
  19. /*reject:promise1: 1000ms*/

promise1是成功的,但不会打印

  1. let promise1 = new Promise((resolve, reject) => {
  2. setTimeout(function () {
  3. resolve('promise1: 1000ms');
  4. }, 1000)
  5. })
  6. let promise2 = new Promise((resolve, reject) => {
  7. setTimeout(function () {
  8. reject('promise2: 2000ms');
  9. }, 2000)
  10. })
  11. let promise3 = new Promise((resolve, reject) => {
  12. setTimeout(function () {
  13. reject('promise3: 3000ms');
  14. }, 3000)
  15. })
  16. let p = Promise.all([promise1, promise2, promise3]);
  17. p.then(res => console.log( res))
  18. .catch(err => console.log('reject:' + err));
  19. /*reject:promise1: 1000ms*/

race

time 1h5m
不管成功失败,只要先执行完就打印出来,后面执行的都不会打印,只打印第一个

  1. let promise1 = new Promise((resolve, reject) => {
  2. setTimeout(function () {
  3. resolve('promise1: 1000ms');
  4. }, 1000)
  5. })
  6. let promise2 = new Promise((resolve, reject) => {
  7. setTimeout(function () {
  8. resolve('promise2: 2000ms');
  9. }, 2000)
  10. })
  11. let promise3 = new Promise((resolve, reject) => {
  12. setTimeout(function () {
  13. resolve('promise3: 3000ms');
  14. }, 3000)
  15. })
  16. let p = Promise.race([promise1, promise2, promise3]);
  17. p.then(res => console.log( res))
  18. .catch(err => console.log('reject:' + err));
  19. /*promise1: 1000ms*/

总结
time 1h13m
all:参数是iter对象
所有都成功,把一个包含iter里的所有返回值的数组,作为成功回调
如果失败,会把iterable里的第一个失败的promise对象的错误信息作为它的失败错误信息;

race
不管成功还是失败都会返回一个promise对象
promise对象也会将成功或失败的详细作为返回值

thenable

time 1h11m43s

time 1h22m54s
对象里面有then可以用Promise.resolve方法转换成promise方法

  1. let thenable = {
  2. then: function (resolve) {
  3. resolve(42);
  4. }
  5. }
  6. let p1 = Promise.resolve(thenable);
  7. p1.then(value => console.log(value));
  8. /*42*/

time 1h36m22s
Promise.resolve(thenable)一般这样用,一般不会用Promise.reject(),讲道理是可以的,但不这样用,不用会,不要这样用

转换字符串

time 1h31m49s
‘hello’转换成了promise方法,因为只有promise才可以.then,用promise包裹,之后再拿到值

  1. let p1 = Promise.resolve('hello');
  2. p1.then(res => console.log(res));
  3. /*hello*/

time 1h33m21s

运用

time 1h38m54s
这是以前的写法,层层嵌套,很麻烦

  1. const fs = require('fs');
  2. fs.readFile('./name.txt', 'utf-8', (err, data) => {
  3. console.log(data);
  4. if (data) {
  5. fs.readFile(data, 'utf-8', (err, data) => {
  6. console.log(data);
  7. if (data) {
  8. fs.readFile(data, 'utf-8', (err, data) => {
  9. console.log(data);
  10. })
  11. }
  12. })
  13. }
  14. })
  15. /*
  16. * ./number.txt
  17. ./score.txt
  18. 90
  19. */

Promise运用

time 1h40m36s
封装promise,之后再.then,

  1. const fs = require('fs');
  2. function readFile(path) {
  3. return new Promise((resolve, reject) => {
  4. fs.readFile(path, 'utf-8', (err, data) => {
  5. if (data) {
  6. resolve(data);
  7. }
  8. })
  9. })
  10. }
  11. readFile('./name.txt')
  12. .then((data) => {
  13. // console.log(data);
  14. return readFile(data);
  15. })
  16. .then(data => {
  17. return readFile(data);
  18. })
  19. .then(data => console.log(data))
  20. /*90*/

把函数promise化

time 1h52m32s

  1. const fs = require('fs')
  2. function promisify(func) {
  3. return function (...arg) {
  4. return new Promise((resolve, reject) => {
  5. func(...arg, (err, data) => {
  6. if (err) {
  7. reject(err);
  8. } else {
  9. resolve(data);
  10. }
  11. })
  12. })
  13. }
  14. }
  15. let readFile = promisify(fs.readFile);
  16. readFile('./name.txt', 'utf-8')
  17. .then(data => readFile(data, 'utf-8'))
  18. .then(data => readFile(data, 'utf-8'))
  19. .then(data => console.log(data))
  20. /*90*/

time 2h0m
这个函数在nodejs8.0封装到nodejs中了,可以不用自己写了
这个东西比较常用,所以集成到了nodejs中,用直接用就行了,但也得会写,并不是很难

  1. const fs = require('fs')
  2. const util = require('util');
  3. let readFile = util.promisify(fs.readFile);
  4. readFile('./name.txt', 'utf-8')
  5. .then(data => readFile(data, 'utf-8'))
  6. .then(data => readFile(data, 'utf-8'))
  7. .then(data => console.log(data))
  8. /*90*/

time 2h4m7s
要写许多行转换,如果想转换多个对象,想用promise包装多个方法,得写许多行,也很麻烦

  1. const fs = require('fs')
  2. const util = require('util');
  3. let readFile = util.promisify(fs.readFile);
  4. let readdir = util.promisify(fs.readdir);
  5. let writeFile = util.promisify(fs.writeFile);
  6. readFile('./name.txt', 'utf-8')
  7. .then(data => readFile(data, 'utf-8'))
  8. .then(data => readFile(data, 'utf-8'))
  9. .then(data => console.log(data))
  10. /*90*/

image.png
想改变这些函数
time 2h6m52s

image.png
image.png

image.png