原来写的一个分页查询,回调了好几层。

exports.list = function(req,res) {

    var params = {};
var current_page = common_util.get_param_value(req,'current_page','Number',1);
var page_size = common_util.get_param_value(req,'page_size','Number',10);
var start_index = common_util.get_start_index(current_page,page_size); Topic.find(params).skip(start_index).limit(page_size).sort({'reply_time':-1}).exec(function(err,docs){
if(err) {
console.error(err);
}else {
Topic.count(params,function(err,count){
if(err){
console.error(err);
}else{
docs.forEach(function(doc){
doc.interval = date_util.interval(doc.last_reply_time);
}); Category.find({}, function (err, categoryList) {
if (err) {
console.error(err);
} else {
var result = {};
result.current_page = current_page;
result.page_size = page_size;
result.total_count = count;
resutl.total_page = common_util.get_total_page(count,page_size);
result.datas = docs;
result.categoryList = categoryList;
console.dir("hello");
console.dir(result);
res.render('index', result);
}
});
}
});
}
});
}

使用Async后,代码如下:

exports.list = function(req,res) {
var params = {};
var current_page = common_util.get_param_value(req,'current_page','Number',1);
var page_size = common_util.get_param_value(req,'page_size','Number',10);
var start_index = common_util.get_start_index(current_page,page_size);
async.parallel({
datas:function(cb){
Topic.find(params).skip(start_index).limit(page_size).sort({'reply_time':-1}).exec(function(err,docs){
cb(err,docs);
});
},
total_count:function(cb){
Topic.count(params,function(err,count){
cb(err,count);
});
},
categoryList:function(cb){
Category.find({}, function (err, categoryList) {
cb(err,categoryList);
});
}
},function(err,results){
if(err){
console.error(err);
}else{
results.current_page = current_page;
results.page_size = page_size;
results.total_page = common_util.get_total_page(results.total_count,page_size);
res.render('index', results);
}
});
}
async.parallel,并行执行。支持json格式任务,返回的数据也是json格式,json字段的顺序按照任务字段的排序。
{ datas:
[ { _id: 539c156dce6d3ea40f1f1d15,
author: 'yekai',
content: '<p>\n\t<span style="font-family:微软雅黑, Tahoma, Verdana, 宋体;font-size:16px;line-height:26px;background-color:#FFFFFF;">由于网站刚刚建立,并没有提供什么特别的内容,仅仅是将官方主页的内容翻译过来而已,而且也有部分的错误,不过相信以后该网站会成为了解Ubuntu在中国的动态的一个窗 口。</span>\n</p>\n<p>\n\t<span style="font-family:微软雅黑, Tahoma, Verdana, 宋体;font-size:16px;line-height:26px;background-color:#FFFFFF;"><img src="http://static.cnbetacdn.com/newsimg/2014/0613/25_1jCLSOQac.png_w600.png" alt="" /><br />\n</span>\n</p>',
category: 'other',
title: 'Ubuntu 中国官网现已面世',
__v: 1,
last_reply_time: Tue Jun 17 2014 21:54:33 GMT+0800 (中国标准时间),
last_reply_username: 'yekai',
reply_amount: 2,
},
{ _id: 53a04500e5cb4710178753a5,
last_reply_username: 'yekai',
last_reply_time: Tue Jun 17 2014 21:44:52 GMT+0800 (中国标准时间),
author: 'yekai',
content: '赌球输了',
category: 'world_cup',
title: '德国赢了',
__v: 2,
reply_amount: 3,
},
{ _id: 539c16fe9e359e841cc1b097,
author: 'yekai',
content: '<p>\n\t<span style="color:#434343;font-family:微软雅黑, Tahoma, Verdana, 宋体;font-size:16px;line-height:24px;background-color:#FBFBFB;">据国外媒体报道,研究人员最新研究表明,地球最大蓄水层位于美国境内之下的地幔层,大约在地下643.7公里(400英里)处。</span>\n</p>\n<p>\n\t<span style="color:#434343;font-family:微软雅黑, Tahoma, Verdana, 宋体;font-size:16px;line-height:24px;background-color:#FBFBFB;"><img src="http://static.cnbetacdn.com/newsimg/2014/0614/51_1jCUT0h0z.jpg_w600.jpg" alt="" /><br />\n</span>\n</p>',
category: 'other',
title: '美国地下643.7公里发现地球最大“蓄水池”',
__v: 2,
last_reply_time: Tue Jun 17 2014 22:06:43 GMT+0800 (中国标准时间),
last_reply_username: 'afei',
reply_amount: 3,
} ],
total_count: 3,
categoryList:
[ { _id: 5397181a1f7f534418cbbd84,
desc: 'Java类别',
code: 'Java',
name: 'Java',
__v: 0 },
{ _id: 53971d90e209ea6414b2819f,
desc: '',
code: 'Nodejs',
name: 'Nodejs',
__v: 0 },
{ _id: 5399b429a068211816605fbb,
desc: '',
code: 'world_cup',
name: '世界杯',
__v: 0 },
{ _id: 539c1526ce6d3ea40f1f1d14,
desc: '',
code: 'other',
name: '其他',
__v: 0 } ],
current_page: 1,
page_size: 10,
total_page: 1 }
 

避免多层回调,Node.js异步库Async使用(parallel)的更多相关文章

  1. 避免多层回调,Node.js异步库Async使用(series)

    未使用Async之前coffeescript写的代码: exports.product_file_add = (req,res) -> if !req.param('file_id') retu ...

  2. Node.js异步处理CPU密集型任务

    Node.js异步处理CPU密集型任务 Node.js擅长数据密集型实时(data-intensive real-time)交互的应用场景.然而数据密集型实时应用程序并非仅仅有I/O密集型任务,当碰到 ...

  3. node.js异步编程的几种模式

    Node.js异步编程的几种模式 以读取文件为例: 1.callback function const fs = require('fs'); //callback function fs.readF ...

  4. Node.js 基础库

    全局对象 Node.js 中的全局对象是 global,所有全局变量(除了 global 本身以外)都是 global对象的属性. 我们在 Node.js 中能够直接访问到对象通常都是 global ...

  5. 深入理解node.js异步编程:基础篇

    ###[本文是基础内容,大神请绕道,才疏学浅,难免纰漏,请各位轻喷] ##1. 概述 目前开源社区最火热的技术当属Node.js莫属了,作为使用Javascript为主要开发语言的服务器端编程技术和平 ...

  6. node.js异步编程解决方案之Promise用法

    node.js异步编程解决方案之Promise var dbBase = require('../db/db_base'); var school_info_db = require('../db/s ...

  7. node js 异步运行流程控制模块Async介绍

    1.Async介绍 sync是一个流程控制工具包.提供了直接而强大的异步功能.基于Javascript为Node.js设计,同一时候也能够直接在浏览器中使用. Async提供了大约20个函数,包含经常 ...

  8. node.js 异步式I/O 与事件驱动

    Node.js 最大的特点就是异步式 I/O(或者非阻塞 I/O)与事件紧密结合的编程模式.这种模式与传统的同步式 I/O 线性的编程思路有很大的不同,因为控制流很大程度上要靠事件和回调函数来组织,一 ...

  9. Node.js 异步异闻录

    本文首发在个人博客:http://muyunyun.cn/posts/7b9fdc87/ 提到 Node.js, 我们脑海就会浮现异步.非阻塞.单线程等关键词,进一步我们还会想到 buffer.模块机 ...

随机推荐

  1. Android 三种方式实现自定义圆形页面加载中效果的进度条

    转载:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=76872 一.通过动画实现 定义res/anim/loading.xml如 ...

  2. Request Session生命周期及struts1 中service的编写

    现在接手的项目是一个早期的struts1框架的项目.同时也是刚开始接触web 以及struts1架构. 在处理多个action时,有一个tab子页面需要每5s自动刷新一次. 然后在测试过程中发现,点击 ...

  3. HackerRank "Poisonous Plants"

    I had the same BFS idea as one of the AC code this: because dying pattern is spead from the initial ...

  4. HackerRank "Array and simple queries" !

    The most interesting, flexible and juicy binary tree problem I have ever seen. I learnt it from here ...

  5. 【ntp】centos7下ntp服务器设置

    安装ntp #检查服务是否安装 rpm -q ntp #安装ntp服务器 yum -y install ntp 修改配置文件:/etc/ntp.conf 内容如下: restrict default ...

  6. 154 Find Minimum in Rotated Sorted Array II

    多写限制条件可以加快调试速度. ======= Follow up for "Find Minimum in Rotated Sorted Array":What if dupli ...

  7. shell下root用户切换其他用户运行程序

    工作中,一些程序,需要随机启动,但是不是以root用户运行,于是需要在rc.local中通过shell,从root用户切换到其他用户运行程序,命令如下: su -c 'command' - user ...

  8. JQuery validate 在IE兼容模式下出现 js错误(成员找不到)的修正:

    JQuery validate 在IE兼容模式下 下出现 js错误(成员找不到)的修正: // Add novalidate tag if HTML5. //this.attr( "nova ...

  9. PLSQL_基础系列12_替换函数用法REPLACE / TRANSLATE / REGEXP_REPLACE

    20150806 Created By BaoXinjian

  10. NeHe OpenGL教程 第四十三课:FreeType库

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...