luciotato / waitfor

Sequential programming for node.js, end of callback hell / pyramid of doom
MIT License
532 stars 29 forks source link

Sequential ? #30

Closed fabioce closed 9 years ago

fabioce commented 9 years ago

I need a real sequential, for example see this your example modified to return value

var dns = require("dns"), wait=require('wait.for');

function test(){ var addresses = wait.for(dns.resolve4,"google.com"); for (var i = 0; i < addresses.length; i++) { var a = addresses[i]; console.log("reverse for " + a + ": " + JSON.stringify(wait.for(dns.reverse,a))); } return (a);

}

var a= wait.launchFiber(test); console.log(a);


undefined reverse for 74.125.232.136: ["mil02s05-in-f8.1e100.net"] reverse for 74.125.232.137: ["mil02s05-in-f9.1e100.net"] reverse for 74.125.232.130: ["mil02s05-in-f2.1e100.net"] reverse for 74.125.232.135: ["mil02s05-in-f7.1e100.net"] reverse for 74.125.232.129: ["mil02s05-in-f1.1e100.net"] reverse for 74.125.232.128: ["mil02s05-in-f0.1e100.net"] reverse for 74.125.232.134: ["mil02s05-in-f6.1e100.net"] reverse for 74.125.232.133: ["mil02s05-in-f5.1e100.net"] reverse for 74.125.232.142: ["mil02s05-in-f14.1e100.net"] reverse for 74.125.232.132: ["mil02s05-in-f4.1e100.net"] reverse for 74.125.232.131: ["mil02s05-in-f3.1e100.net"]

the console.log at the end work before the function. There is code to have value at the end in my flow. I understat that node.js isn't the language to do that, but I need this sequentiality only for a few of things.

luciotato commented 9 years ago

launchFiber is "launch and forget". You need to launch an intermediate "sequential":

var dns = require("dns"), wait=require('wait.for');

function test(){
    var addresses = wait.for(dns.resolve4,"google.com");
    for (var i = 0; i < addresses.length; i++) {
        var a = addresses[i];
        console.log("reverse for " + a + ": " + JSON.stringify(wait.for(dns.reverse,a)));
    } 
    return a;
}

function sequentialMainFunction(){
    a = test();
    console.log(a);
}

wait.launchFiber(sequentialMainFunction);
fabioce commented 9 years ago

Thanks, it work.