lazy.js icon indicating copy to clipboard operation
lazy.js copied to clipboard

Async iteration

Open calvinmetcalf opened this issue 12 years ago • 3 comments

a better model for async iteration might be something like the async library's version where the iterator function can deal with async things, so for a contrived example say I wanted to download files with the scheme http://fakestuff.nope/blahblah0.json, http://fakestuff.nope/blahblah1.json,... being able to do something like

var result = Lazy
    .generate(function(a){
        return a;//am I missing an easier way to do this?
    })
    .async()
    .map(function(value,index,callback){
        $.ajax('http://fakestuff.nope/blahblah'+value+'.json').then(callback);
    })
    .take(5)

though that doesn't actually work without some sort of promise implementation so something like

Lazy
    .generate(function(a){return a;})
    .async()
    .map(function(value,index,callback){
        $.ajax('http://fakestuff.nope/blahblah'+value+'.json').then(callback);
    })
    .take(5,function(result){
        //do stuff with result
    });

while we're at it the .async method could be used for how many to do at a time

Lazy
    .generate(function(a){return a;})
    .async(3)//3 at a time
    .map(function(value,index,callback){
        $.ajax('http://fakestuff.nope/blahblah'+value+'.json').then(callback);
    })
    .take(5,function(result){
        //do stuff with result
    });

this could be pretty helpful if in the example we only wanted one of them, knew it was under 500, and had some value

Lazy
    .range(0,500)
    .async()
    .map(function(value,index,callback){
        $.ajax({url:'http://fakestuff.nope/blahblah'+value+'.json').then(callback);
    })
    .find(function(a){
        return a.someKey === someValue;
    })

as far as I can tell the idea of an async iteration here is that you don't care about the return of the iterator.

You could also do this with promises instead of callbacks, which would probably be more inline with the library but that's a different can of worms.

calvinmetcalf avatar May 31 '13 12:05 calvinmetcalf

+1 on promise support in all the places where it makes sense.

I think the fluidity of promises fits well with Lazy.js. I'm using ES6 style arrow functions (in TypeScript), it is would be neat 'functional' looking code:

Lazy
    .range(0, 100)
    .promise(3) //3 at a time
    .map((value, index) => {
        // now we simply return a promise (a then()-able object)
        return http.get('http://example.com/' + value + '.json');
    })
    .lines()
    .take(5)
    .then((result) => {
        // the then() callback would trigger a evaluation and receive the result
        doStuff(result);
    });

This would be pretty amazing if it works smoothly (especially if you could easily switch between the regular sync version, the streams-api and the classic-async like OP described).

Bartvds avatar Dec 15 '13 02:12 Bartvds

+1 the world needs this.

juancampa avatar Jan 07 '15 08:01 juancampa

Just bumped into this, and surprised it doesn't work with lazy.js... :/

DanielJoyce avatar Aug 22 '18 17:08 DanielJoyce