国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

基于promise.js實現nodejs的promises庫

2019-11-20 14:22:23
字體:
來源:轉載
供稿:網友

今天從GIT源碼庫中下載了promise.js,發現該源碼是基于Web前端JavaScript寫的,并不能直接用于nodejs。還好代碼不是很多,也不是很復雜。經過分析整合,將其實現為nodejs的一個框架,代碼如下:

(function(){/*** Copyright 2012-2013 (c) Pierre Duquesne <stackp@online.fr>* script: promise.js* description: promises的nodejs模塊* modified: https://github.com/stackp/promisejs* authors: alwu007@sina.cn* */var Promise = exports.Promise = function(){  this._callbacks = [];};Promise.prototype.then = function(func, context){  //處理回調結果的方法  function doCallbackResults(r) {    if (r instanceof Promise) {      r.then(function(err, values){        p.done(err, values);      });    } else {      p.done(null, r);    }  }  var p = new Promise();  if (this._isdone) {    var results = func.apply(context, this.results);    doCallbackResults(results);  } else {    this._callbacks.push(function(){      var results = func.apply(context, arguments);      doCallbackResults(results);    });  }  return p;};Promise.prototype.done = function(){  this.results = arguments;  this._isdone = true;  for (var i=0; i<this._callbacks.length; i++) {    this._callbacks[i].apply(null, arguments);  }  this._callbacks = [];};Promise.join = function(promises){  var p = new Promise();  var results = [];  if (!promises || !promises.length) {    p.done(results);    return p;  }  var numdone = 0;  var total = promises.length;  function notifier(i) {    return function() {      numdone += 1;      results[i] = Array.prototype.slice.call(arguments);      if (numdone === total) {        p.done(results);      }    };  }  for (var i = 0; i < total; i++) {    promises[i].then(notifier(i));  }  return p;};Promise.chain = function(funcs, args) {  var p = new Promise();  if (!funcs || !funcs.length) {    p.done.apply(p, args);  } else {    funcs[0].apply(null, args).then(function(){      funcs.splice(0, 1);      Promise.chain(funcs, arguments).then(function(){        p.done.apply(p, arguments);      });    });  }  return p;};})();

另附測試代碼如下:

/*** script: test.js* description: promise.js測試代碼* */var promise = require('./mypromise');function asyncfoo() {  var p = new promise.Promise();  setTimeout(function(){    p.done();  }, 1000);  return p;}function syncfoo() {  var p = new promise.Promise();  p.done();  return p;}var o = {};/*asyncfoo().then(function(){  return 'Raymond';}, o).then(function(err, name){  o.name = name;  return asyncfoo().then(asyncfoo).then(function(){    return asyncfoo().then(asyncfoo).then(function(){      return 18;    });  });}, o).then(function(err, age){  o.age = age;  return asyncfoo().then(asyncfoo).then(function(){    return asyncfoo().then(asyncfoo).then(function(){      return 'boy';    });  }).then(function(err, sex){    return sex;  });}).then(function(err, sex){  o.sex = sex;  return 'Hello, world!';}).then(function(err, say){  o.say = say;  console.dir(o);});syncfoo().then(function(){  return 'Raymond';}, o).then(function(err, name){  o.name = name;  return syncfoo().then(syncfoo).then(function(){    return syncfoo().then(syncfoo).then(function(){      return 18;    });  });}, o).then(function(err, age){  o.age = age;  return asyncfoo().then(asyncfoo).then(function(){    return asyncfoo().then(asyncfoo).then(function(){      return 'boy';    });  }).then(function(err, sex){    return sex;  });}).then(function(err, sex){  o.sex = sex;  return 'Hello, world!';}).then(function(err, say){  o.say = say;  console.dir(o);});*/function asyncfoo1(){  var p = new promise.Promise();  setTimeout(function(){    p.done(null, 'Raymond');  }, 1000);  return p;}function asyncfoo2(err, name){  o.name = name;  var p = new promise.Promise();  setTimeout(function(){    p.done(null, 18);  }, 1000);  return p;}function asyncfoo3(err, age){  o.age = age;  var p = new promise.Promise();  setTimeout(function(){    p.done(null, 'boy');  }, 1000);  return p;}function asyncfoo4(){  var p = new promise.Promise();  setTimeout(function(){    p.done(null, 'Hello, world!');  }, 1000);  return p;}promise.Promise.chain([asyncfoo1, asyncfoo2, asyncfoo3]).then(function(err, sex){  o.sex = sex;  return asyncfoo4();}).then(function(err, say){  o.say = say;}).then(function(){  console.dir(o);});

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南召县| 长武县| 乌审旗| 彭阳县| 寿阳县| 精河县| 札达县| 宜兰市| 华容县| 额敏县| 尼木县| 石台县| 昭觉县| 高台县| 青神县| 芦山县| 大庆市| 澳门| 东海县| 武宁县| 荃湾区| 吉安市| 尼勒克县| 南召县| 六枝特区| 武强县| 建水县| 怀安县| 昌江| 左权县| 稷山县| 梓潼县| 辛集市| 响水县| 曲沃县| 普安县| 常德市| 石阡县| 德化县| 舞阳县| 台山市|