luciotato / waitfor

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

Question: How to have wait.launchFiber return value be set to a variable? #32

Closed JemiloII closed 9 years ago

JemiloII commented 9 years ago

So I'm trying to set my wait.launchFiber's return value to be set to a variable. Essentially, I request a webpage, get data, process the data, then return the processed data. This way I have a single function to call to request and process the data.

working

'use strict';

var request = require('request'),
    wait = require('wait.for');

var getPage = function (url) {
    var data = wait.for(request, url);
    console.log(data.body);
    return data.body;
};

wait.launchFiber(getPage, 'http://google.com/');

doesn't work

'use strict';

var request = require('request'),
    wait = require('wait.for');

var getPage = function (url) {
    var data = wait.for(request, url);
    console.log(data.body);
    return data.body;
};

var storePage = wait.launchFiber(getPage, 'http://google.com/');
console.log(storePage);

trying to have storePage return the data that wait.launchFiber gets.

luciotato commented 9 years ago

wait.launchFiber is "launch and forget". It returns almost immediately.

do this:

'use strict';

var request = require('request'),
    wait = require('wait.for');

var getPage = function (url) {
    var data = wait.for(request, url);
    console.log(data.body);
    return data.body;
};

function mainFiber(){
   var page = getPage('http://google.com/');
   console.log(page);
}

wait.launchFiber(mainFiber);
JemiloII commented 9 years ago

Thanks for the fast reply! Is there another method to launch and remember so I could store the info into a variable. The goal is to have the data return back to what called it.

somevariable = wait.launchFiber(mainFiber);

console.log(somevariable);
// somevariable now stores the google.com html

I'm really striving for that result. I want the returns in the function waited on to be returned like wait.for does.

luciotato commented 9 years ago

Yes, check the example in my previous answer.

to get a result, you use var data = wait.for(...) and also any function having wait.for in its body as in var page = getPage('http://google.com/') in function mainFiber().

the result you're striving for will be in var page

Try the example in my previous answer.

JemiloII commented 9 years ago

no you missed what I wanted to accomplish, I want to return page so that somevariable will have the value of page. I do not want to be locked within a function. i want to be able to make easy functions to use later on. were I just say google = getPage('http://google.com') and google is the google.com page contents.

http://stackoverflow.com/questions/6048504/synchronous-request-in-nodejs

this is what sparked my inspirations.

luciotato commented 9 years ago

I understand what you want, but node.js is single threaded (main loop) and async, so you cannot do what you want, unless you're in a Fiber.

wait.launchFiber will put you inside a Fiber, from there you CAN do google = getPage('http://google.com'), but, and I repeat, you can only do it inside a fiber.