美文网首页
nodejs异步控制

nodejs异步控制

作者: darr250 | 来源:发表于2016-11-28 21:58 被阅读0次

bluebird与原生Promise对象及bluebird模块的中文API文档

nodejs异步控制「co、async、Q 、『es6原生promise』、then.js、bluebird」有何优缺点?最爱哪个?哪个简单?

http爬虫中用到bluebirdapi

var http = require("http");
var cheerio = require("cheerio");
var Promise = require("bluebird");
var url = "http://www.imooc.com/learn/348";
var baseUrl = "http://www.imooc.com/learn/";
var courseIdArray = [
    "637",
    "348",
    "259",
    "197",
    "134"
];
//promiseFiles : promise集合
var promiseFiles = [];
courseIdArray.forEach(function(item){
    promiseFiles.push(getPageData(baseUrl + item))
})
Promise.all(promiseFiles).then(function(pages) {
    pages.forEach(function(item, index){
        console.log("--------------------第" + index + "个课程--------------------")
        printData(filterchapter(item));
    })
});
function getPageData(url){
    return new Promise(function(resolve, reject){
        http.get(url, function(res){
            var html = "";
            res.on("data", function(data){
                html += data;
            })
            res.on("end", function(){
                resolve(html);
                /*var htmlData = filterchapter(html);
                printData(htmlData);*/
            })
        }).on("error", function(e){
            reject(e);
            console.log("获取课程数据错误");
        })
    })
}
function filterchapter(html){
    var $ = cheerio.load(html);
    var chapters = $(".chapter");
    var chapterDatas = [];
    chapters.each(function(item){
        var chapter = $(this);
        var chapterTitle = chapter.find("strong").text().replace(/[\r\n\s]/g,"");
        var chapterData = {
            title: chapterTitle,
            videos: []
        }
        var videos = chapter.find(".video").children("li");
        videos.each(function(item){
            var video = $(this);
            var videoTitle = video.find(".J-media-item").text().replace(/[\r\n\s]/g,"");
            var videoId = video.find(".J-media-item").attr("href").split("video/")[1].replace(/[\r\n\s]/g,"");
            chapterData.videos.push({
                title: videoTitle,
                id: videoId
            })
        })
        chapterDatas.push(chapterData)
    })
    return chapterDatas;
}
function printData(data){
    data.forEach(function(item){
        var title = item.title;
        console.log("chapterTitle: " + title);
        item.videos.forEach(function(item){
            console.log("[" + item.id + "]:"  + item.title);
        })
    })
}

相关文章

  • nodejs异步控制

    bluebird与原生Promise对象及bluebird模块的中文API文档 nodejs异步控制「co、asy...

  • 作用域、事件、异步

    作用域、事件、异步 1. 课程介绍 Ø CNPM(淘宝NPM) Ø NodeJS控制台 Ø NodeJS作用域(掌...

  • Nodejs学习笔记-异步流程控制

    安装async npm install async --g报错:npm: relocation error: np...

  • Async流程控制学习

    今天学习nodeJS深入浅出的第四章。node异步控制有提到Async的强大流程控制能力,我觉得有必要学习下,所以...

  • 02.NodeJs基本语法

    一.认识NodeJs 1.NodeJs的事件驱动和异步IO 缺点: 2.npm介绍: ​ 是nodejs自带的...

  • JS 模块化方案对比

    1. CommonJS 规范(同步加载 NodeJS) 2. AMD(异步加载模块 requireJS) 采用异步...

  • NodeJS基础原理

    NodeJS基础原理 异步IO原理浅析及优化方案 异步IO的好处(输入输出input output) 前端通过异步...

  • 链接

    webpack新手教程 javascript标准库 异步请求 nodejs的安装nodejs的升级淘宝npm vu...

  • 大规模NodeJS项目架构与优化

    聊聊大规模NodeJS项目架构 NodeJS异步IO原理浅析及优化方案 NodeJS内存管理机制及内存优化 大规模...

  • nodejs_回调函数的理解

    nodejs中的函数回调直接体现了nodejs的异步的思想,从java写到nodejs,这种思想可能比较影响对代码...

网友评论

      本文标题:nodejs异步控制

      本文链接:https://www.haomeiwen.com/subject/wizopttx.html