zoukankan      html  css  js  c++  java
  • JavaScript中的回调地狱及解决方法

    1、回调地狱

    在使用JavaScript时,为了实现某些逻辑经常会写出层层嵌套的回调函数,如果嵌套过多,会极大影响代码可读性和逻辑,这种情况也被成为回调地狱。比如说你要把一个函数 A 作为回调函数,但是该函数又接受一个函数 B 作为参数,甚至 B 还接受 C 作为参数使用,就这样层层嵌套,人称之为回调地狱,代码阅读性非常差。比如:

    var sayhello = function (name, callback) {
      setTimeout(function () {
        console.log(name);
        callback();
      }, 1000);
    }
    sayhello("first", function () {
      sayhello("second", function () {
        sayhello("third", function () {
          console.log("end");
        });
      });
    });
    //输出: first second third  end

    2、解决回调地狱

    解决回调地狱有很多方法,比如:Promise 对象、Generator 函数、async 函数

    3、Promise 对象解决回调地狱

    采用链式的 then,可以指定一组按照次序调用的回调函数。这时,前一个 then 里的一个回调函数,返回的可能还是一个 Promise对象(即有异步操作),这时后一个回调函数,就会等待该 Promise对象的状态发生变化,才会被调用。由此实现异步操作按照次序执行。

    var sayhello = function (name) {
      return new Promise(function (resolve, reject) {
        setTimeout(function () {
          console.log(name);
          resolve();  //在异步操作执行完后执行 resolve() 函数
        }, 1000);
      });
    }
    sayhello("first").then(function () {
      return sayhello("second");  //仍然返回一个 Promise 对象
    }).then(function () {
      return sayhello("third");
    }).then(function () {
      console.log('end');
    }).catch(function (err) {
      console.log(err);
    })
    //输出:first  second  third end

    上面代码中,第一个 then 方法指定的回调函数,返回的是另一个Promise对象。这时,第二个then方法指定的回调函数,就会等待这个新的Promise对象状态发生变化。如果变为resolved,就继续执行第二个 then 里的回调函数

    4、Generator 函数

    // 用 co 模块自动执行
    var co = require('co');
    var sayhello = function (name, ms) {
      setTimeout(function (name,ms) {
        console.log(name);
      }, ms);
    }
    
    var gen = function* () {
      yield sayhello("xiaomi", 2000);
      console.log('frist');
      yield sayhello("huawei", 1000);
      console.log('second');
      yield sayhello("apple", 500);
      console.log('end');
    }
    co(gen());
  • 相关阅读:
    实践出真知关于ios项目重命名的实践 (xcode 4.3.1) ,以及svn 导出项目命令
    解决error: linker command failed with exit code 1类似的错误
    关于对ios 目录路径的理解
    UITableView 隔行换色 选中背景色 取消选中颜色 返回后显示正常颜色
    NSLog的常用格式说明小释
    app打包总结 以及 提交app审核过程
    UIView圆角
    substringToIndex substringFromIndex
    ios 判断文字高度,适用于tableview的自定义高度
    [转]android解析XML总结
  • 原文地址:https://www.cnblogs.com/wenxuehai/p/10455664.html
Copyright © 2011-2022 走看看