Handling async requests. How?

28 views
Skip to first unread message

Ronnie Paskin

unread,
May 16, 2012, 4:17:11 PM5/16/12
to futures-j...@googlegroups.com
Hi,

Sorry for the noob question, but I'm trying to wrap my head around using the futures library and I'm getting some strange behavior.

In a nutshell, I want to be able to:
1. Issue several asynchronous requests in parallel
2. When all of these are done, run a modifier on all the result sets
3. When the modifier is done, return the result

Here's something that (kind of) worked (note that of course <my local server> is a real working server url):

 app.get("/futures", function(req, res){

    var count = 0,
        timers = [
            5000,
            5011,
            10000
        ];

    forEachAsync(timers, function (next, time) {

        console.log('Requesting ',time, '>>>>>>>>>>>>>>>>>>>>>>>');
        setTimeout(function(){
            request('<my localserver went here>/?t='+time, function(){
                console.log(count += 1, time);
                console.log('Done! ',time, '<<<<<<<<<<<<<<<<<');

                if (count == timers.length){
                    console.log("=============== ALL CALLBACKS DONE ==============");
                    res.send("All Done ==============");
                }
            })
        }, time);

        next();
    }).then(function () {
        console.log('Loop done ======');
    });

});


But it doesn't seem very clean.

I tried to use chanify, like so:

function doAsyncRequest (time, p){
    console.log('Setting timeout! ',time, '.........');
    setTimeout(function(){
        console.log('Requesting ',time, '>>>>>>>>>>>>>>>>>');
        request('<my local server>/?t='+time, function(){
            console.log('Done! ',time, '<<<<<<<<<<<<<<<<<');
            p.fulfill();
        })
    }, time);
}

app.get("/chainify", function(req, res){

    var Contacts,
        providers,
        modifiers,
        consumers;

    providers = {
        all: function(params) {
            var p = Futures.promise();
            doAsyncRequest (5000, p);
            return p.passable();
        },
        one: function(id, params) {
            var p = Futures.promise();
            doAsyncRequest (7000, p);
            return p.passable();
        },
        two: function(id, params) {
            var p = Futures.promise();
            doAsyncRequest (7011, p);
            return p.passable();
        }
    }

    modifiers = {};

    consumers = {
        display: function (data, params) {
            console.log(data, params);
            res.send("All Done?");
        }
    };

    console.log(178);
    Contacts = Futures.chainify(providers, modifiers, consumers);
    console.log(180,Contacts);
    Contacts.all().display();
    console.log(182);
}

But I get:

Error: You have upgraded to Futures 2.x. See http://github.com/coolaj86/futures for details.
    at Object.upgradeMessage...
...

I'm sure I'm just doing something silly, but it feels like hitting my head against the wall. Any tips? What would be the cleanest, recommended, implementation for something basic like that?

thanks in advance,
Ronnie

Ronnie Paskin

unread,
May 17, 2012, 10:05:00 AM5/17/12
to futures-j...@googlegroups.com
Noticed this doesn't seem to be very active anymore. Going for jQuery's built-in implementation instead.

AJ ONeal

unread,
May 17, 2012, 11:55:24 AM5/17/12
to futures-j...@googlegroups.com
Nope, not a dead project at all, but I work full time and have volunteer work after work a few days a week, so I'm not always at my inbox.

I think you were using old documentation.

If you want to do a small number of things in parallel, use join:

If you want to do a large number of things in parallel, use lateral:

If you want to do a number of things in sequence, use sequence or forEachAsync:

To build / package:


npm install -g pakmanager uglify-js
cd your/project/dir
npm init # and fill out basic project details
npm install -S jQuery underscore forEachAsync
pakmanager build
uglifyjs pakmanaged.js > pakmanaged.min.js

And then in the browser

include pakmanaged.min.js in your script tag.

var forEachAsync = require('forEachAsync')
  ;

You can package jQuery, underscore, and other libraries along with futures using packmanager.

AJ ONeal

On Thu, May 17, 2012 at 8:05 AM, Ronnie Paskin <rpa...@gmail.com> wrote:
Noticed this doesn't seem to be very active anymore. Going for jQuery's built-in implementation instead.

--
Please use prefixes: [Pony] Feature requests, [TestCase] Test cases, [Bug] if you're lazy. If you have contract work or position for FuturesJS devs, prefix [Bounty] or [Job].
-----
To post to this group, send email to futures-j...@googlegroups.com
To unsubscribe from this group, send email to
futures-javascr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/futures-javascript?hl=en

Reply all
Reply to author
Forward
0 new messages