ES6-----promise的详细用法

约定不等于承诺〃 2022-01-28 00:35 409阅读 0赞

01.promise基本操作

  1. <!--
  2. Created by wangyang on 2019-05-22.
  3. itwangyang@gmail.com
  4. http://www.itwangyang.xyz
  5. -->
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  12. <meta name="apple-mobile-web-app-capable" content="yes">
  13. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  14. <meta name="format-detection" content="telephone=no">
  15. <title>Title</title>
  16. <meta name="description" content="">
  17. <meta name="keywords" content="">
  18. <!--所有的IE都起作用:-->
  19. <!--[if IE]> <link rel="stylesheet" type="text/css" href="all-ie-only.css/> <![endif]-->
  20. </head>
  21. <body>
  22. <script>
  23. /*
  24. 为什么要有promise:解决(回调地狱)的问题
  25. 回调地狱:
  26. //跟以前的if条件地狱很像
  27. // if(){
  28. // if(){
  29. // if(){
  30. // }
  31. // }
  32. // }
  33. $.get("/getUser",function(res){
  34. $.get("/getUserDetail",function(){
  35. $.get("/getCart",function(){
  36. $.get("/getBooks",function(){
  37. //...
  38. })
  39. })
  40. })
  41. })
  42. //node开发:读取文件;开个服务器、接收一个请求、请求路径、访问数据库
  43. * */
  44. //把异步操作封装在一个promise对象中
  45. function fn() {
  46. return new Promise(function (resolve,reject) {
  47. setTimeout(()=> {
  48. console.log("您好");
  49. //其实异步逻辑,到这款其实已经执行完毕了,就可以告诉外界,可以执行其他操作了,如果让外界得知?
  50. resolve();
  51. },1000)
  52. })
  53. }
  54. //调用了这个函数,
  55. fn().then(res =>{
  56. //执行到下一步
  57. console.log("下一步");
  58. fn().then(res=>{
  59. console.log("执行第二步")
  60. })
  61. })
  62. //输出顺序:
  63. //你好
  64. //下一步
  65. //你好
  66. //执行第二步
  67. </script>
  68. </body>
  69. </html>

02-1.promise如任何解决回调地狱的

  1. <!--
  2. Created by wangyang on 2019-05-22.
  3. itwangyang@gmail.com
  4. http://www.itwangyang.xyz
  5. -->
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  12. <meta name="apple-mobile-web-app-capable" content="yes">
  13. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  14. <meta name="format-detection" content="telephone=no">
  15. <title>Title</title>
  16. <meta name="description" content="">
  17. <meta name="keywords" content="">
  18. <!--所有的IE都起作用:-->
  19. <!--[if IE]> <link rel="stylesheet" type="text/css" href="all-ie-only.css/> <![endif]-->
  20. </head>
  21. <body>
  22. <script>
  23. function f1() {
  24. return new Promise(resolve => {
  25. setTimeout(()=>{
  26. console.log("第一步");
  27. //异步逻辑已经执行完,必须要告诉外界我执行完了
  28. resolve();
  29. },1000)
  30. })
  31. }
  32. function f2() {
  33. return new Promise(resolve => {
  34. setTimeout(()=>{
  35. console.log("第二步");
  36. //异步逻辑已经执行完,必须要告诉外界我执行完了
  37. resolve();
  38. },1000)
  39. })
  40. }
  41. f1().then(res=>{
  42. //返回一个新的promise对象,然后链式调用then方法
  43. return f2();
  44. }).then(res=>{
  45. return f1();
  46. }).then(res=>{
  47. return f2();
  48. }).then(res=>{
  49. console.log("结束");
  50. })
  51. /*
  52. Promise函数基本用法
  53. var promise=new Promise((resolve,reject)=>{
  54. //b 把需要执行的异步操作放在这里
  55. $.get("/getUser",res=>{
  56. //获取数据的异步操作已经执行完毕了,等待下一步的执行,通过执行resolve函数,告诉外界你可以执行下一步操作了
  57. //c、
  58. resolve(res)
  59. //而执行的下一步操作,其实就是写在then的回调函数中的
  60. })
  61. })
  62. //a、
  63. promise.then(res=>{
  64. //d、执行后续的操作
  65. console.log(res);
  66. })
  67. */
  68. </script>
  69. </body>
  70. </html>

03-2.promise传参

  1. <!--
  2. Created by wangyang on 2019-05-22.
  3. itwangyang@gmail.com
  4. http://www.itwangyang.xyz
  5. -->
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  12. <meta name="apple-mobile-web-app-capable" content="yes">
  13. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  14. <meta name="format-detection" content="telephone=no">
  15. <title>Title</title>
  16. <meta name="description" content="">
  17. <meta name="keywords" content="">
  18. <!--所有的IE都起作用:-->
  19. <!--[if IE]> <link rel="stylesheet" type="text/css" href="all-ie-only.css/> <![endif]-->
  20. </head>
  21. <body>
  22. <script>
  23. function getUser() {
  24. return new Promise(resolve => {
  25. $.get("/getUser",res=>{
  26. //res是从服务端中接收到的数据
  27. //把数据传到下一步操作中
  28. //告诉外界本次的异步操作已经执行完毕了
  29. resolve(res);
  30. });
  31. })
  32. }
  33. getUser().then(res=>{
  34. //res就表示上一个异步操作返回的参数值:从服务器中获取的数据
  35. })
  36. </script>
  37. </body>
  38. </html>

03-3 promis错误处理

  1. <!--
  2. Created by wangyang on 2019-05-22.
  3. itwangyang@gmail.com
  4. http://www.itwangyang.xyz
  5. -->
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  12. <meta name="apple-mobile-web-app-capable" content="yes">
  13. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  14. <meta name="format-detection" content="telephone=no">
  15. <title>Title</title>
  16. <meta name="description" content="">
  17. <meta name="keywords" content="">
  18. <!--所有的IE都起作用:-->
  19. <!--[if IE]> <link rel="stylesheet" type="text/css" href="all-ie-only.css/> <![endif]-->
  20. </head>
  21. <body>
  22. <script>
  23. function getBook() {
  24. return new Promise((resolve,reject) => {
  25. $.ajax({
  26. url:'/getBook',
  27. success(res){
  28. //成功获取数据
  29. resolve(res);//resolve();表示异步操作是成功的
  30. },
  31. error(resError){//res表示错误信息
  32. //如果失败就会执行
  33. reject(resError);//reject();表示异步操作是失败的
  34. }
  35. });
  36. })
  37. }
  38. /* //第一种处理错误的方式
  39. getBook().then(res=>{
  40. //这里的res是表示请求成功时候获取到的数据
  41. },resError=>{
  42. console.log(resError);
  43. });
  44. */
  45. //第二种处理错误的方式
  46. getBook().then(res=>{
  47. //成功了
  48. }).catch(res=>{
  49. //这里也可以获取到错误信息
  50. })
  51. //上面2种错误处理的方式,第二种更加推荐
  52. //第二种方式更强大的地方在于:
  53. //a、不仅仅可以捕获到reject传递的参数
  54. //b、还可以捕获到:成功的回调中发生的错误
  55. </script>
  56. </body>
  57. </html>

04-4.promise捕获错误

  1. <!--
  2. Created by wangyang on 2019-05-22.
  3. itwangyang@gmail.com
  4. http://www.itwangyang.xyz
  5. -->
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  12. <meta name="apple-mobile-web-app-capable" content="yes">
  13. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  14. <meta name="format-detection" content="telephone=no">
  15. <title>Title</title>
  16. <meta name="description" content="">
  17. <meta name="keywords" content="">
  18. <!--所有的IE都起作用:-->
  19. <!--[if IE]> <link rel="stylesheet" type="text/css" href="all-ie-only.css/> <![endif]-->
  20. </head>
  21. <body>
  22. <script>
  23. function f1(name) {
  24. return new Promise((resolve,reject) => {
  25. setTimeout(()=>{
  26. if (name == "a"){
  27. resolve("成功");
  28. } else {
  29. reject("失败");
  30. }
  31. },1000)
  32. })
  33. }
  34. // f1("a").then(res=>{console.log(res)});
  35. f1("b").then(res=>{
  36. console.log(res)
  37. }).catch(res=>{
  38. console.log("失败了");
  39. })
  40. </script>
  41. </body>
  42. </html>

05-5.promise多层异步使用catch

  1. <!--
  2. Created by wangyang on 2019-05-22.
  3. itwangyang@gmail.com
  4. http://www.itwangyang.xyz
  5. -->
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  12. <meta name="apple-mobile-web-app-capable" content="yes">
  13. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  14. <meta name="format-detection" content="telephone=no">
  15. <title>Title</title>
  16. <meta name="description" content="">
  17. <meta name="keywords" content="">
  18. <!--所有的IE都起作用:-->
  19. <!--[if IE]> <link rel="stylesheet" type="text/css" href="all-ie-only.css/> <![endif]-->
  20. </head>
  21. <body>
  22. <script>
  23. new Promise((resolve,reject)=>{
  24. setTimeout(()=>{
  25. console.log("第一步");
  26. resolve("第一步完成");
  27. },100)
  28. }).then(res=>{
  29. console.log(res);
  30. return new Promise((resolve, reject) => {
  31. setTimeout(()=>{
  32. console.log("第二步");
  33. reject("第二步失败");
  34. },100)
  35. })
  36. }).then(res=>{
  37. console.log(res);
  38. console.log("不会执行到这里");
  39. }).catch(res=>{
  40. console.log(res);
  41. console.log("失败");
  42. });
  43. //fetch是新浏览器自带的。。。。。。。。。。。。。。。。
  44. //axios就是一个基于Promise封装出来的进行ajax请求的库
  45. axios.get("/getUser").then(res=>{
  46. return axios.get("/getUserDetail");
  47. })
  48. </script>
  49. </body>
  50. </html>

06-6.promise里面的返回值

  1. <!--
  2. Created by wangyang on 2019-05-22.
  3. itwangyang@gmail.com
  4. http://www.itwangyang.xyz
  5. -->
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  12. <meta name="apple-mobile-web-app-capable" content="yes">
  13. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  14. <meta name="format-detection" content="telephone=no">
  15. <title>Title</title>
  16. <meta name="description" content="">
  17. <meta name="keywords" content="">
  18. <!--所有的IE都起作用:-->
  19. <!--[if IE]> <link rel="stylesheet" type="text/css" href="all-ie-only.css/> <![endif]-->
  20. </head>
  21. <body>
  22. <script>
  23. new Promise((resolve, reject)=>{
  24. setTimeout(()=>{
  25. resolve("第一步");
  26. },1000)
  27. }).then(res=>{
  28. return 100;
  29. }).then(res=>{
  30. console.log(res);
  31. })
  32. </script>
  33. </body>
  34. </html>

promise,暂时写到这些,目前就是用到这些

发表评论

表情:
评论列表 (有 0 条评论,409人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ES6 Promise

    Promise promise对象从语法上来看呢,是一个构造函数,用来生成Promise实例,用来封装异步操作,并提供成功后或失败后的结果 所谓Promise,简单说

    相关 ES6 Promise

    promise是一种解决异步函数传递数据问题 回调函数解决---->回调地狱 .Promise Promise是一个对象, 使用时需要创建这个对象 new P

    相关 es6 Promise

    传统实现异步操作就是采用回调函数,回调函数方式本身没有什么问题,但是在多重回调层层嵌套的情况下,那么代码的可阅读性就会出现问题。 Promise对象是一个新的异步操作解决方

    相关 ES6-Promise

    Promise what? 在我看来Promise就相当于一个承诺容器,将所要执行的事件存贮在promise容器中。 Promise在ES6中被统一规范,由浏览