使用Q-promises对代码执行进行排序

时间:2014-08-27 10:43:20

标签: javascript node.js promise nonblocking

我尝试使用node.js对我的Q-promise代码进行排序:

var request = require('request');
var cheerio = require('cheerio');
var Q = require('q');
var companiesUrls = [];
var url = '';
var companiesUrls2 = [];
var carsUrls = [];
var carsOwnerReview = {};
var carReviewUrl = [];
var site = 'http://www.....'
var companiesPath = '/companies'

//step 1
function one() {
    var deferred = Q.defer();
    request(site+companiesPath, function(err, resp, body){
        if(!err && resp.statusCode == 200){
            console.log("Step 1 request was passed!");
            var $ = cheerio.load(body);
            $('a', '#group-content').each(function(){
                var url = $(this).attr('href');
                companiesUrls.push(url);
            });
        };
    });
    console.log("step 1 is finished");
    deferred.resolve();
    return deferred.promise;
};

//step 2
function two() {
    var deferred = Q.defer();
    for(var i=0;i<companiesUrls.length;i+=2){
        companiesUrls2.push(companiesUrls[i]);
    };
    console.log("step 2 is finished");
    deferred.resolve();
    return deferred.promise;
};

//step 3
function three() {
    var deferred = Q.defer();
    for(var i=0;i<companiesUrls2.length;i++){
        request(site+companiesUrls2[i], function(err, resp, body){
            if(!err && resp.statusCode == 200){
                console.log("Step 3 request was passed!");
                var $ = cheerio.load(body);
                $('h3.edition-title').children().children().each(function(){
                    var url = $(this).attr('href');
                    carsUrls.push(url);
                });
            };
        });
    };
    console.log("step 3 is finished");
    deferred.resolve();
    return deferred.promise;
};

//step 4
function four() {
    var deferred = Q.defer();
    for(var i=0;i<carsUrls.length;i++){
        carReviewUrl.push(carsUrls[carsUrls.length-1].slice(0,-7)+'/owner-reviews');
    };
    console.log("step 4 is finished");
    deferred.resolve();
    return deferred.promise;
};

//step 5
function five() {
    var deferred = Q.defer();
    for(var i=0;i<carReviewUrl.length;i++){
        request(site+carReviewUrl[i], function(err, resp, body){
            if(!err && resp.statusCode == 200){
                console.log("Step 5 request was passed!");
                var $ = cheerio.load(body);
                var model = $('#page-title').text();
                $('span.total-votes').children().each(function(){
                    var reviewNum = $(this).text();
                    carsOwnerReview[model] = reviewNum;
                });
            };
        });
    };
    console.log("step 5 is finished");
    deferred.resolve();
    return deferred.promise;
};

//step 6
function six() {
    var deferred = Q.defer();
    var keysSorted = Object.keys(carsOwnerReview).sort(function(a,b){return carsOwnerReview[a]-carsOwnerReview[b]});
    var keysSortedReversed = keysSorted.reverse();
    console.log("step 6 is finished");
    console.log(carsOwnerReview);
    deferred.resolve();
    return deferred.promise;
};

one()
    .then(two)
    .then(three)
    .then(four)
    .then(five)
    .then(six);

因此,为了使我的程序正常工作,我需要按排序顺序运行此函数,因为所有这些函数都相互依赖。这是我的预期输出:

"Step 1 request was passed!"
"step 1 is finished"
"step 2 is finished"
"Step 3 request was passed!"
"step 3 is finished"
"step 4 is finished"
"Step 5 request was passed!"
"step 5 is finished"
"step 6 is finished"
{...}

这是我的程序产生的输出:

"step 1 is finished"
"step 2 is finished"
"step 3 is finished"
"step 4 is finished"
"step 5 is finished"
"step 6 is finished"
{}
"Step 1 request was passed!"

为什么会发生这种情况,我该如何解决?

0 个答案:

没有答案