Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要收集Javascript节点的异步调用_Javascript_Node.js - Fatal编程技术网

需要收集Javascript节点的异步调用

需要收集Javascript节点的异步调用,javascript,node.js,Javascript,Node.js,我正在使用nodejs的tumblr模块访问tumblr。看起来很有趣,但我对javascript和nodejs还不熟悉。我需要以下概念的帮助。假设我打了两个电话: var someCrapArrayWhichINeedFilled = new Array(); tumblr.get('/posts/photo', {hostname: 'scipsy.tumblr.com', limit:3}, function(json){ console.log(json)

我正在使用nodejs的tumblr模块访问tumblr。看起来很有趣,但我对javascript和nodejs还不熟悉。我需要以下概念的帮助。假设我打了两个电话:

var someCrapArrayWhichINeedFilled = new Array(); tumblr.get('/posts/photo', {hostname: 'scipsy.tumblr.com', limit:3}, function(json){ console.log(json); someCrapArrayWhichINeedFilled.push(json); }); tumblr.get('/posts/photo', {hostname: 'vulpix-bl.tumblr.com', limit:3}, function(json){ console.log(json); someCrapArrayWhichINeedFilled.push(json); }); var somecraparraywhichinedfilled=新数组(); get('/posts/photo',{hostname:'scipsy.tumblr.com',limit:3},函数(json){ log(json); somecraparraywhichinedfilled.push(json); }); get('/posts/photo',{hostname:'vulpix-bl.tumblr.com',limit:3},函数(json){ log(json); somecraparraywhichinedfilled.push(json); }); 现在我知道回调就是回调,它们会在启动时启动。所以问题是他们实际上是如何开火的。他们什么时候真正开火。如何填充数组以便使用它


再次,我需要从两个不同的博客上拍摄三张照片,然后在我的网页上返回。我的服务器和客户端都是javascript。因此,请告诉我它在javascript世界中是如何实现的,以及哪些库可以用于此目的

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

/**
 * @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;
        }
    });
});

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

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
});

你就是那个人。。一些他已经准备好的垃圾是非常非常好的触摸。。现在我更了解这一点…顺便说一句,你的回答帮助我理解了这将如何工作。。然后你可以用四个空格缩进来格式化你的代码。见: