Closed shellscape closed 9 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
I tried the above and it worked great. Thanks.
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
}
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);
});
});
}
Thanks Lucio, I am new to JavaScript and Node.js. That not only helps with your package, but with JavaScript in general. :)
Closing due to age.
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):
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 callapp
manually afterwait.launchFiber();
? I've tried the following, with no success:Following the above example, wherever I call wait.for() I get the error message
wait.for can only be called inside a fiber
.