原promise.js库地址:https://github.com/stackp/promisejs

promises是JavaScript实现优雅编程的一个非常不错的轻量级框架。该框架可以让你从杂乱的多重异步回调代码中解脱出来,并把精力集中到你的业务逻辑上。

今天从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);
});

实现nodejs的promises库(基于promise.js改写)的更多相关文章

  1. [译]基于Vue.js的10个最佳UI框架,用于构建移动应用程序

    原文查看10 Best Vue.js based UI Frameworks for Building Mobile Apps 如果您期待使用Vue.js构建移动应用程序,那么您可以选择许多可用的UI ...

  2. queue-fun —— nodejs下基于Promise的队列控制模块。

    工作告一段落,闲来无事,写了一个在nodejs实现“半阻塞”的控制程序. 一直以来,nodejs以单线程非阻塞,高并发的特性而闻名.搞这个“半阻塞”是东西,有什么用呢? 场景一: 现在的web应用可有 ...

  3. axios - 基于 Promise 的 HTTP 异步请求库

    axios 是基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用.Vue 更新到2.0之后,作者就宣告不再对 vue-resource 模块更新,而是推荐使用 a ...

  4. Axios 是一个基于 promise 的 HTTP 库

    Axios 是一个基于 promise 的 HTTP 库 vue项目中关于axios的简单使用 axios介绍 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.j ...

  5. 基于promise用于浏览器和node.js的http客户端的axios

    axios 是一个基于Promise 用于浏览器和 nodejs 的 HTTP 客户端,它本身具有以下特征: 从浏览器中创建 XMLHttpRequest 从 node.js 发出 http 请求 支 ...

  6. RSuite 一个基于 React.js 的 Web 组件库

    RSuite http://rsuite.github.io RSuite 是一个基于 React.js 开发的 Web 组件库,参考 Bootstrap 设计,提供其中常用组件,支持响应式布局. 我 ...

  7. 【360开源】thinkjs:基于Promise的Node.js MVC框架 (转)

    thinkjs是360奇舞团开源的一款Node.js MVC框架,该框架底层基于Promise来实现,很好的解决了Node.js里异步回调的问题.360奇舞团(奇虎75Team),是奇虎360公司We ...

  8. Vue项目中使用基于Vue.js的移动组件库cube-ui

    cube-ui 是滴滴公司的技术团队基于 Vue.js 实现的精致移动端组件库.很赞,基本场景是够用了,感谢开源!感谢默默奉献的你们. 刚爬完坑,就来总结啦!!希望对需要的朋友有小小的帮助. (一)创 ...

  9. KoaHub平台基于Node.js开发的Koa 连接支付宝插件代码信息详情

    KoaHub平台基于Node.js开发的Koa 链接支付宝插件代码信息详情 easy-alipay alipay payment & notification APIs easy-alipay ...

随机推荐

  1. apache 服务器端口被IIS服务器占用

    今天遇到一个问题 同事机子上安装了wamp运行环境,所有服务也打开了,把dede系统放到了www目录下面,但是打开localhost网址,一直是跳转到一个IIS的web服务器主页 想不到是什么问题,又 ...

  2. python自动开发之第二十一天

    一.请求周期 url> 路由 > 函数或类 > 返回字符串或者模板语言? 1.Form表单提交: 提交 -> url > 函数或类中的方法 - .... HttpResp ...

  3. App Store生存指南

    资格获取   如果已经有App Store开发帐号请跳过此节.   App Store的资格获取其实一直以来都不算难,和其它事情一样,需要的只是耐心.现在苹果对申请者的文书手续要求已经比几年前简化多了 ...

  4. gcc将多个静态库链接成一个静态库

    参考:https://sourceware.org/binutils/docs/binutils/ar-scripts.html#ar-scripts makefile如下: ARSCRIPT=scr ...

  5. linux中VI编辑器使用个人记录

    VI编辑器有三种编辑模式:命令模式.最后行模式.文本编辑模式 启动VI后进入的第一种模式是”命令模式“.从命令模式可进入最后行模式和编辑模式.而后两种模式之间不能直接切换.必须按ESC键退回到命令模式 ...

  6. BZOJ 3091 城市旅行

    Description Input Output Sample Input 4 5 1 3 2 5 1 2 1 3 2 4 4 2 4 1 2 4 2 3 4 3 1 4 1 4 1 4 Sample ...

  7. JFS 文件系统概述及布局分析

    JFS 文件系统概述及布局分析 日志文件系统如何缩短系统重启时间 如果发生系统崩溃,JFS 提供了快速文件系统重启.通过使用数据库日志技术,JFS 能在几秒或几分钟之内把文件系统恢复到一致状态,而非日 ...

  8. c语言小练习(蛮好玩的)

    1.求三个数的平均数,要求保留三位小数位 #include <conio.h> #include<stdio.h> int main(){ int a,b,c; float a ...

  9. vim学习与理解

    1. 转变思维 --> vim 无鼠标文本编辑工具 在鸟哥的linux私房菜中,是这么说明linux的: 1. vim是linux like系统中非常强大的一个文本编辑工具,历史悠久,很多系统都 ...

  10. 【转】NDK编译可执行文件在Android L中运行显示error: only position independent executables (PIE) are supported.失败问题解决办法。

    原文网址:http://blog.csdn.net/hxdanya/article/details/39371759 由于使用了NDK编译的可执行文件在应用中调用,在4.4及之前的版本上一直没出问题. ...