q.js实现nodejs顺序调用
nodejs的异步调用有时候是最让人头疼的,如何能是一些代码顺序的执行呢,这里和大家分享nodejs的promise
什么是promise
promise
一个标准,它描述了异步调用的返回结果,包括正确返回结果和错误处理。关于详细的说明文档可以参考Promises/A+。目前实现promise
标准的模块有很多,如Q、bluebird和Deferred,下面我们以Q为例,介绍一下promise
在nodejs
中的使用方法。
我查找了关于promise的使用,其中最好用的就是q.js了,个人觉得。当然还有promise.js,有兴趣的朋友可以研究一下,这里主要说一下q.js的用法。
首先下载安装q.js——
npm install q
1、使用Q.nfcall
相对于Q.fcall ,Q.nfcall 就是node 的Q.fcall。
var FS = require('fs'),
Q = require('q'),
colors = require('colors'),
file = 'example.txt';
var fsReadFile = Q.nfcall(FS.readFile,file,encoding);
fsReadFile.then(function(result){
console.log((“invoke in nfcall ” + file).red);
console.log(result.green);
},function(error){
console.log(“invoke in nfcall”.red);
console.log(error.toString().red);
}
);
Q.fcall(function () {
return "1";
})
.then(function(value2){
console.log("打印", value2);
return User.test(value2);
})
.then(function (value3) {
console.log(value3);
return "3";
})
.then(function (value4) {
console.log(value4);
return "4";
})
.then(function (value4) {
// Do something with value4
console.log("显示:", value4);
})
.catch(function (error) {
// Handle any error from all above steps
})
.done();
2使用Q.denodeify
var fsReadFile_denodeify = Q.denodeify(FS.readFile);
fsReadFile_denodeify(file,encoding).then(function(result){
console.log("invoke in denodeify".red);
console.log(result.green)
},function(error){
console.log("invoke in denodeify".red);
console.log(error.toString().red);
}
);
3、使用Q.deferd
var fsReadFile_deferd = function(file,encoding){
var deferred = Q.defer();
FS.readFile(file,encoding,function(error,result){
if(error){
deferred.reject(error.toString().red);
}
deferred.resolve(result);
});
return deferred.promise;
};
fsReadFile_deferd(file).then(function(result){
console.log("invoke in deferd".red);
console.log(result.toString().green);
},function(error){
console.log("invoke in deferd".red);
console.log(error.toString().red);
}
);
4、使用makeNodeResolver()
var fsReadFile_makeNodeResolver = function(file,encoding){
var deferred = Q.defer();
FS.readFile(file,encoding,deferred.makeNodeResolver());
return deferred.promise;
};
fsReadFile_makeNodeResolver(file,encoding).then(function(result){
console.log("invoke in makeNodeResolver".red);
console.log(result.green);
},function(error){
console.log(error.toString().red);
});
q.js实现nodejs顺序调用的更多相关文章
- Road to the future——伪MVVM库Q.js
模仿Vuejs的伪MVVM库,下面是使用说明 项目地址:https://github.com/miniflycn/Q.js 相关项目:https://github.com/miniflycn/Ques ...
- 借助Q.js学习javascript异步编程。
金字塔式 //add1 function step1(n, callback) { setTimeout(function(){ callback.call(null, n + 1); }, 100) ...
- 在python中调用js或者nodejs
在python中调用js或者nodejs要使用PyExecJs第三方包. pip install pyexecjs 示例代码 >>> import execjs >>&g ...
- nodejs Q.js promise
var Q = require("q"); documentation for Qhttps://github.com/kriskowal/qhttps://github.com/ ...
- note.js之 Nodejs+Express4在windows下的配置
本篇主要介绍一下在windows平台下采用nodejs+express4框架+Mongodb实现网站的开发.其实本人是不赞同在Windows平台下使用node.js进行开发,但由于公司后台工程师都是采 ...
- Backbone.js 和 Nodejs 的一些共同点搞不清楚
前端方面 我用 Backbone.js 做过前端的开发,印象里就是后端按模型对象的属性把 JSON 数据发过来,我写在模板里渲染就好了 模板加载( underscore.js ) 建立模型 渲染视图 ...
- 【原创】分布式之数据库和缓存双写一致性方案解析(三) 前端面试送命题(二)-callback,promise,generator,async-await JS的进阶技巧 前端面试送命题(一)-JS三座大山 Nodejs的运行原理-科普篇 优化设计提高sql类数据库的性能 简单理解token机制
[原创]分布式之数据库和缓存双写一致性方案解析(三) 正文 博主本来觉得,<分布式之数据库和缓存双写一致性方案解析>,一文已经十分清晰.然而这一两天,有人在微信上私聊我,觉得应该要采用 ...
- 用好js与nodejs中的try...catch
对异常的捕获和处理是提高程序鲁棒性的一个重要方式,即使在javascript/nodejs等看似“很难写出bug”的弱类型语言里,异常捕获处理仍至关重要,这主要是因为: 1.在一个代码块里,如果程序运 ...
- 前端开发 Vue Vue.js和Nodejs的关系
首先vue.js 是库,不是框架,不是框架,不是框架. Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据. Vue.js 的核心是一个允许你 ...
随机推荐
- LoadRunner如何在注册业务脚本中设置参数化唯一性
LR在录制一个网站注册业务的脚本时,突然间遇到一个问题:注册时,由于注册用户需要验证唯一性,所以在LR回放脚本时,用Run-time Viewer工具回放可以发现(先在脚本中设置几个断点),真实运行的 ...
- 有关docker新版的icc、iptables的一个巨坑
之前玩过docker的icc=false.iptables=true 按照这两个参数配置之后,想指定两个特定的容器通讯,直接用--link即可. 但最近我在下载了1.12新版的docker,这个不奏效 ...
- android XML解析之DOM解析方式
DOM 解析方式步骤: 第一步:首选需要获得DOM解析器工厂实例 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ...
- EF Code Frist
EF:学习资料 http://www.cnblogs.com/libingql/category/366833.html
- C#中精确计时的一点收获
以下所有代码运行环境:Windows 2003, Intel(R) Core(TM) 2 Duo CPU E8400 @ 3.00GHz 2.99GHz,2.96GB内存 根据综合网上的一些文章,精 ...
- Haproxy+PXC实现负载均衡
软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第 三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载.HA ...
- myEclipse + phonegap-2.9.0 总跳出3个脚本提示
环境:myEclipse + phonegap-2.9.0按照教程全部完毕后,浏览页面时,总会跳出3个脚本提示:1:gap:["Device","getDeviceInf ...
- lda模型的python实现
LDA(Latent Dirichlet Allocation)是一种文档主题生成模型,最近看了点资料,准备使用python实现一下.至于数学模型相关知识,某度一大堆,这里也给出之前参考过的一个挺详细 ...
- requirejs--源码分析
/*---------------------------------------start-------------------------------*/ req({}); // num == 1 ...
- 使用Hashtable和List结合拼json数据
在做项目的时候,有时候需要向页面返回一个特定的json类型的数据,一般情况下会有下面的方法进行拼接: public String chongzhiList() throws Exception { L ...