0

我正在使用 nodejs 的 tumblr 模块来访问 tumblr。有趣的是,我似乎是 javascript 和 nodejs 的新手。我需要以下概念的帮助。假设我打了这两个电话:

var someCrapArrayWhichINeedFilled = new Array();
tumblr.get('/posts/photo', {hostname: 'scipsy.tumblr.com', limit:3}, function(json){
                控制台.log(json);
                someCrapArrayWhichINeedFilled.push(json);
            });
tumblr.get('/posts/photo', {hostname: 'vulpix-bl.tumblr.com', limit:3}, function(json){
                控制台.log(json);
                someCrapArrayWhichINeedFilled.push(json);
            });

现在我知道回调是回调,它们会在触发时触发。所以问题是他们实际上是如何开火的。他们什么时候真正开火。我如何填充一个数组以便我可以使用它。

同样,我需要从两个不同的博客中拍摄三张照片,然后应该在我的网页上返回。我的服务器和客户端都在 javascript 中。因此,请告诉我正确的想法,它是如何在 javascript 世界中完成的,以及可以为此目的使用哪些库。

4

2 回答 2

0

使用模块重写代码async,如下所示:

var async = require('async');

async.parallel([
    function (callback) {
        tumblr.get('/posts/photo', {hostname: 'scipsy.tumblr.com', limit:3}, function(json){
        console.log(json);
        callback(false, json);
    },
    function (callback) {
        tumblr.get('/posts/photo', {hostname: 'vulpix-bl.tumblr.com', limit:3}, function(json){
        console.log(json);
        callback(false, json);
    }
], function (err, someCrapArrayWhichIsAlreadyFilled) {
    //do work
});
于 2012-08-31T09:45:27.997 回答
0

这是我个人用于查询可变数量的“类型”(导致单独的查询)的请求。我计算请求的类型数量,收集查询响应并在它们全部收集后立即触发回调:

/**
 * @param payLoad -  Object, expected to contain:
 *   @subparam type - Array, required.
 *   @subparam location - Array, contains [lat,lng], optional
 *   @subparam range - Int, optional
 *   @subparam start - String, optional
 * @param cbFn - Function, callBack to call when ready collecting results.
 */
socket.on('map', function (payLoad, cbFn) {
    if (typeof cbFn === 'undefined') cbFn = function (response) {};
    var counter = 0,
        totalTypes = 0,
        resultSet = [];
    if (typeof payLoad.type === 'undefined' || !(payLoad.type instanceof Array)) {
        cbFn({error : "Required parameter 'command' was not set or was not an array"});
        return;
    }
    totalTypes = payLoad.type.length;

    /**
     * MySQL returns the results in asynchronous callbacks, so in order to pass
     * the results back to the client in one callBack we have to
     * collect the results first and combine them, which is what this function does.
     *
     * @param data - Object with results to pass to the client.
     */
    function processResults (data) {
        counter++;
        //Store the result untill we have all responses.
        resultSet.push(data);
        if (counter >= totalTypes) {
            //We've collected all results. Pass them back to the client.
            if (resultSet.length > 0) {
                cbFn(resultSet);
            } else {
                cbFn({error : 'Your query did not yield any results. This should not happn.'});
            }
        }
    }

    //Process each type from the request.
    payLoad.type.forEach(function (type) {
        switch (type.toLowerCase()) {
            case "type-a":
                mysqlClient.query("SELECT super important stuff;",
                    function selectCallBack (err, results) {
                        if (!err && results.length > 0) {
                            processResults({type : 'type-a', error : false, data : results});
                        } else {
                            processResults({type : 'type-a', error : "No results found"});
                        }
                    });
                break;
            case "type-b":
                mysqlClient.query('SELECT other stuff',
                    function selectCallBack (err, results) {
                        if (!err && results.length > 0) {
                            processResults({type : 'type-b', error : false, data : results});
                        } else {
                            processResults({type : 'type-b', error : "No results found"});
                        }
                    });
                break;
            default:
                processResults({type : type, error : 'Unrecognized type parameter'});
                break;
        }
    });
});
于 2012-08-31T09:33:34.360 回答