阮一峰文档 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 function strFun (str ) { return new Promise ((resolve, reject ) => { if (str.length > 5 ){ console .log(str); anotherStr = 'Good!' ; return resolve(anotherStr); console .log('after resolve' ) } else { console .log(str + ': This string is too short!' ); return reject('Bad!' ); } }) } strFun('Hello World!' ).then((anotherStr ) => { console .log(anotherStr); },(err ) => { console .error(err); }) strFun('Hi!' ).then((anotherStr ) => { console .log(anotherStr); },(err ) => { console .error(err); }) function timeOut (ms ) { return new Promise ((resolve, reject ) => { setTimeout (resolve, ms, 'Done!' ); }) } timeOut(2000 ).then((value ) => { console .log(value); }) const p1 = new Promise ((resolve, reject ) => { setTimeout (() => { return reject(new Error ('Fail!' )) }, 3000 ); }) const p2 = new Promise ((resolve, reject ) => { setTimeout (() => { return resolve(p1); }, 1000 ); }) p2 .then((msg ) => { console .log(msg); }) .catch((err ) => { console .error(err); }) const strArr = ['How are you' , 'Fine thank you' , 'Nice to meet you' ]; strFun(strArr[0 ]).then( () => { strFun(strArr[1 ]); }, (err ) => { console .error(err); } ).then( () => { strFun(strArr[2 ]); return 114514 ; }, (err ) => { console .error(err); } ).then( (msg ) => { console .log(msg); } ) strFun('I love u, u love me, mexue ice-cream and tea~' ).finally( () => { console .log('OK' ); } ) strFun('lmao' ).finally( () => { console .log('OK' ); } )
尚硅谷视频 Promise.resolve
如果传入的参数为非Promise对象,则返回的结果为成功Promise对象
如果传入的参数为Promise对象,则参数的结果决定了返回的结果
1 2 3 4 5 6 7 8 let p1 = Promise .resolve(111 )let p2 = Promise .resolve(new Promise ((res, rej)) => { rej('Error' ) }) p2.catch(err => { console .log(err) })
Promise.reject 和Promise.resolve相比,无论传入什么类型的数据,返回结果都为失败Promise对象,传入什么,失败的内容就是什么
Promise.all 传入包含n个Promise的数组,返回一个新的Promise,只有所有的Promise都成功才成功,否则失败
若成功,将成功值保存在一个数组中;若失败,只将第一个失败的值保存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 let p1 = new Promise ((res, rej ) => { res('OK' ) }) let p2 = Promise .resolve('Success' )let p3 = Promise .resolve('Oh Yeah' )const result = Promise .all([p1, p2, p3])result.then(value => { console .log(value) }) result.catch(err => { console .log(err) })
Promise.race 和all类似,不同之处:第一个执行完毕的Promise的结果决定了Promise.race返回值的结果