异步代码的处理方案

  1. // 模拟请求
  2. function request(url){
  3. return new Promise((resolve,reject)=>{
  4. setTimeout(()=>{
  5. resolve(url)
  6. },2000);
  7. })
  8. }
  9. /* 方案一:多次回调 */
  10. // 回调地狱
  11. request('aaa').then(res=>{
  12. request(res+'bbb').then(res=>{
  13. request(res+'ccc').then(res=>{
  14. console.log(res)
  15. }
  16. })
  17. })
  18. /* 方案二:Promise中then的返回值来解决 */
  19. // 阅读性差,需要对每段代码块都需阅读
  20. request('aaa').then(res=>{
  21. return request(res+'bbb')
  22. }).then(res=>{
  23. return request(res+'ccc')
  24. }).then(res=>{
  25. console.log(res)
  26. })
  27. /* 方案三:Promise+Generator */
  28. function* getData(){
  29. const res1=yield request('aaa')
  30. const res2=yield request(res1+'bbb')
  31. const res3=yield request(res2+'ccc')
  32. console.log(res3)
  33. }
  34. // 手动调用
  35. const generator=getData()
  36. generator.next().value.then(res1=>{
  37. generator.next(res1).value.then(res2=>{
  38. generator.next(res2).value.then(res3=>{
  39. generator.next(res3)
  40. })
  41. })
  42. })
  43. // 自动调用
  44. /* execGenerator函数早在14-16年TJ就开源于github上,库名叫co */
  45. function execGenerator(genFn){
  46. const generator=genFn()
  47. return exec();
  48. function exec(res){
  49. const result=generator.then(res)
  50. // 结束
  51. if(result.done){
  52. return result.value
  53. }
  54. // 递归
  55. result.value.then(res=>exec(res)
  56. )
  57. }
  58. }
  59. execGenerator(getData)
  60. /* 方案四:async-await */
  61. // 实质是Promise+Generator语法糖
  62. async function getData(){
  63. const res1=await request('aaa');
  64. const res2=await request('bbb');
  65. const res3=await request('ccc');
  66. console.log(res3)
  67. }

异步函数 async-await

本质是Promise+Generator语法糖

async(asynchronous):异步、非同步

sync(synchronous):同步、同时

定义异步函数

  1. async function foo(){···}
  2. const bar=async()=>{···}
  3. class Person{
  4. async foo(){···}
  5. }

与同步函数的区别

1.返回值

异步函数的返回值一定是一个Promise对象

  1. async function foo(){
  2. // 1.返回普通值
  3. return 123
  4. // 2.返回promise
  5. return new Promise((resolve,reject)=>{
  6. resolve(123)
  7. })
  8. // 3.返回thenable
  9. return {
  10. then(resolve,reject){
  11. resolve(123)
  12. }
  13. }
  14. }
  15. foo().then(res=>{
  16. console.log(res) // 123
  17. })

2.异常

  1. async function foo(){
  2. console.log('async start')
  3. throw new Error('errorMsg')
  4. console.log('async end')
  5. }
  6. console.log('sync start')
  7. foo()
  8. console.log('sync end')
  9. /*
  10. sync start
  11. async start
  12. sync end
  13. Uncaught (in promise) Error: errorMsg
  14. */

await关键字

1.await+表达式

表达式同Promise返回值,分三种情况

  1. async function foo(){
  2. /* 情况一:普通值 */
  3. const res1 = await 123
  4. console.log(res2) // 123
  5. /* 情况二:Promise*/
  6. const res2 = await new Promise((resolve,reject)=>{
  7. resolve(345)
  8. })
  9. console.log(res2) // 345
  10. /* 情况三:thenable */
  11. const res3=await {
  12. then(reslove,reject){
  13. resolve(789)
  14. }
  15. }
  16. console.log(res3) // 789
  17. }

2.reject捕获

  1. async function foo(){
  2. await new Promise(resolve,reject){
  3. reject('errorMsg')
  4. }
  5. }
  6. foo().catch(err=>console.log(err))