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 的核心是一个允许你 ...
随机推荐
- MonoDevelop with Visual Studio to Linux and Mac OSX maintaining a single code base for all platforms.
Home | Screenshots | Download | Contact | FAQ | Documentation | Development | Search MonoDevelop i ...
- Android应用程序组件Content Provider的共享数据更新通知机制分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6985171 在Android系统中,应用程序组 ...
- kvm 图形化安装
为了再后续查看方便,我还是完整的记录kvm图形化安装. 介于网络环境的原因,我选择NAT. 2,安装kvm前的准备工作 2.1 关闭防火墙 setenforce 0 vi /etc/sysco ...
- 【SQL语句】 - 在所有存储过程中查找关键字,关键字不区分大小写 [sp_findproc]
USE [EShop]GO/****** Object: StoredProcedure [dbo].[sp_findproc] Script Date: 2015/8/19 11:05:24 *** ...
- 用canvas绘制一个时钟
实现一个时钟的绘制和时间的显示 一,首先是页面的搭建html部分以及一点点的css代码,因为css这块用的比较少,所以就没有单独出来: <!DOCTYPE html> <html l ...
- Lua编译
编译lua包含3部分内容:lua库文件(lua*.lib),lua解释器(lua.exe),lua编译器(luac.exe) 首先: 下载源代码,编译批处理(以5.2.3为例): cd srccl / ...
- TCP/IP详解之:SNMP
基于TCP/IP的网络管理包含3个组成部分: 一个管理信息库MIB:MIB包含所有代理进程的所有可被查询和修改的参数 关于MIB的一套公用的结构和表示符号,即SMI(管理信息结构) 管理进程和代理进程 ...
- Linux下MySql出现#1036 – Table ‘ ‘ is read only 错误解决方法
本文为转载内容,感谢原作者.原文出自:http://zhaoxiaoru39.blog.163.com/blog/static/609552192012511104730115/ 我遇到的问题是:在n ...
- php cli 模式下执行文件,require 加载路径错误
今天,同事突然告诉我,我写的一个做计划任务的php脚本执行总是不成功. 脚本本身很简单,里面只有包含了几个库文件并执行了一个函数,函数应该没有错误,这个函数在别处也调用过,没有问题.我在本地用浏览器访 ...
- JavaScript Infinite scroll & Masonry
// infinitescroll() is called on the element that surrounds // the items you will be loading more of ...