0

nodejsのtumblrモジュールを使用してtumblrにアクセスしています。私はjavascriptとnodejsに慣れていないように見えるかもしれないので面白い。次のコンセプトについてサポートが必要です。これらの2つの呼び出しを行ったとしましょう:

var someCrapArrayWhichINeedFilled = new Array();
tumblr.get('/ posts / photo'、{ホスト名:'scipsy.tumblr.com'、limit:3}、function(json){
                console.log(json);
                someCrapArrayWhichINeedFilled.push(json);
            });
tumblr.get('/ posts / photo'、{ホスト名:'vulpix-bl.tumblr.com'、limit:3}、function(json){
                console.log(json);
                someCrapArrayWhichINeedFilled.push(json);
            });

これで、コールバックはコールバックであり、起動すると起動することがわかりました。したがって、問題は、実際にどのように発砲するかです。彼らはいつ実際に発砲しますか。配列を使用できるようにするには、どうすれば配列にデータを入力できますか。

ここでも、2つの異なるブログから3枚の写真を撮る必要があり、それから私のWebページに戻る必要があります。私のサーバー側とクライアント側はすべて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 に答える