概念

当回调函数里不断嵌套回调函数的 过程就叫回调地狱,即回调函数无限嵌套。这样会导致代码很难去管理和维护。
例子:多个div的顺序淡入

  1. $('div:nth-child(1)').fadeIn(2000, function () {
  2. $('div:nth-child(2)').fadeIn(2000, function () {
  3. $('div:nth-child(3)').fadeIn(2000, function () {
  4. $('div:nth-child(4)').fadeIn(2000, function () {
  5. $('div:nth-child(5)').fadeIn(2000, function () {
  6. $('div:nth-child(6)').fadeIn(2000, function () {
  7. $('div:nth-child(7)').fadeIn(2000, function () {
  8. $('div:nth-child(8)').fadeIn(2000, function () {
  9. });
  10. });
  11. });
  12. });
  13. });
  14. });
  15. });
  16. });
  17. console.log('回调之后的代码');

解决回调函数:可以实现异步代码顺序执行,但又要优雅简洁好管理的代码

  • promise
  • async&await(以promise为基础)