评论

收藏

[JavaScript] 关于defer.promise.then 异步的一个疑问 | 用柯里化做promise | 用递归做promise

开发技术 开发技术 发布于:2021-08-06 12:07 | 阅读数:658 | 评论:0

疑问:感觉会报错,因为执行到defer.promise.then这时候还没到defer.resolve,因为异步读文件,总归会慢
解答:先执行defer.promise.then,是给callback赋值,函数的赋值会比较抽象,callback得到函数的赋值后,是函数就该等待执行,等待resolve的时候,执行,其实也是回调的方式,
关键点:实参是函数,就是形参被复值成函数,把形参当函数看
var Defer = function(){
  var value,callback;
  return {
    resolve :function(val){
      value = val;
      callback(val);
    },
    promise:{
      then:function(fn){
        callback = fn;
      }
    }
  }
}
var fs = require('fs');
var defer = new Defer();
fs.readFile('data/account/accountInfo_0.html',(err,data)=>{
  console.log(2)
  defer.resolve(data)
})
console.log(1)
defer.promise.then(function(resData){
  console.log(3)
  console.log(resData)
});
===============================================
柯里化做promise
var Defer = function(){
  var value,successes=[],errors=[];
  return {
    resolve :function(val){
      successes.forEach(v=>{
        if(!value){
          value = v(val);
        }else{
          value = v(value)
        }
      })
    },
    reject:function(err){
      errors.forEach(v=>{
        v(err)
      })
    },
    promise:{
      then:function(fn){
        successes.push(fn);
        return this;
      },
      catch:function(fn){
        errors.push(fn);
        return this;
      }
    }
  }
}
var fs = require('fs');
function readFile(filter){
  var defer = new Defer();
  fs.readFile(filter,'utf8',(err,data)=>{
    if(err){
      defer.reject(err)
    }else{
      defer.resolve(data)
    }
  })
  return defer.promise;
}
// 当注册成功之后的回掉函数
var promise = readFile('test.txt');//my name is:
promise.then(res=>{
  return res+'liujiny';
}).then(res=>{
  console.log(res);//my name is:liujinyu
  return fs.readFileSync('data/account/accountInfo_0.html');
}).then(res=>{
  console.log(res+'')
}).catch(err=>{
  console.log('err',err)
})
------------------------------------------
用递归做promise
var Defer = function(){
  var success=[],
    errors=[];
  return {
    resolve:function(_value){
      success.forEach(v=>{
        v(_value)
      })
    },
    reject:function(err){
      errors.forEach(v=>{
        v(err)
      })
    },
    promise:{
      then:function(fn){
        var d = new Defer();
        var _fn = function(_value){
          d.resolve(fn(_value));
        }
        success.push(_fn);
        return d.promise;
      },
      catch:function(fn){
        errors.push(fn);
        return this;
      }
    }
  }
}
var fs = require('fs');
function readFile(filter){
  var defer = new Defer();
  fs.readFile(filter,'utf8',(err,data)=>{
    if(err){
      defer.reject(err)
    }else{
      defer.resolve(data)
    }
  })
  return defer.promise;
}
// 当注册成功之后的回掉函数
var promise = readFile('name.txt');//my name is:
promise.then(val=>{
  return val+'liujiny';
}).then(res=>{
  console.log(res);//my name is:liujinyu
  return fs.readFileSync('age.txt');//27
}).then(res=>{
  console.log(res);//27
}).catch(err=>{
  console.log('err',err)
})



关注下面的标签,发现更多相似文章