luciotato / waitfor

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

README Example for use with Express #11

Closed shellscape closed 9 years ago

shellscape commented 10 years ago

I'm still new to the fibers and synchronous concepts of Node, and still learning. One thing I've been wrestling with is how to use your great little lib here with Express. Any chance you might be able to add an example to the readme for Express use?

Here's how an Express app is typically spun up (for those that might read this, and are unfamiliar with Express):


var express = require('express'),
  http = require('http'),
  app = express();

  // lots of config and whatnot

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

Since the callback to http.createServer is handled by express, I'm unsure on where to put the wait.launchFiber(); call and what the parameters passed should be. Is the proper method to use a standard callback and call app manually after wait.launchFiber();? I've tried the following, with no success:


http.createServer(
  function(req, res){
    console.log('req!');
    wait.launchFiber(app, req, res); //app is an instance of express. handle in a fiber
    // keep node spinning
  }).listen(8000);

Following the above example, wherever I call wait.for() I get the error message wait.for can only be called inside a fiber.

luciotato commented 10 years ago

I'm sorry, I don't have enough experience with express, but, look at the first example in: https://github.com/visionmedia/express It seems that you may need to do:

var wait = require('wait.for');
var express = require('express');
var app = express();

// in  a Fiber
function handleGet(req, res){
  res.send( wait.for(fs.readFile,'largeFile.html') );
}

app.get('/', function(req,res){
      wait.launchFiber(handleGet, req, res); //handle in a fiber, keep node spinning
});

app.listen(3000);

Tell me if it works

BobDickinson commented 10 years ago

I tried the above and it worked great. Thanks.

rickskirch commented 9 years ago

My example has lines after the wait.for call and they are never executed. What am i doing wrong?

var wait = require('wait.for');
var _Express = require("express");
var _expressApp = _Express();

_expressApp.get('/create', function (req, res) {
  wait.launchFiber(create, req, res);
});

function Login(req) {
  //login to soap client
  _Soap.confluenceLogIn(_confluenceSoapUrl, function(err, client) {
    //var description = client.describe();
    client.login({ user: 'user', password: 'password' }, function(err1, result) {
      console.log(result.loginReturn.$value);
      return result.loginReturn.$value;
    });
  });
} 

function create(req, res) {
  //log in to confluence
  var token = wait.for(Login, req);
  console.log(token); //never executed
  res.send(token);  //never executed
}
luciotato commented 9 years ago

You "wait.for" any standard async function, so if you want to wait.for Login(), it should be a standard async function:

function Login(req,callback) { // callback(err,data)
  //login to soap client
  _Soap.confluenceLogIn(_confluenceSoapUrl, function(err, client) {
    //var description = client.describe();
    if (err) return callback(err);
    client.login({ user: 'user', password: 'password' }, function(err1, result) {
      console.log(result.loginReturn.$value);
      return callback(err1,result.loginReturn.$value);
    });
  });
} 
rickskirch commented 9 years ago

Thanks Lucio, I am new to JavaScript and Node.js. That not only helps with your package, but with JavaScript in general. :)

shellscape commented 9 years ago

Closing due to age.