maxtaco / tamejs

JavaScript code rewriter for taming async-callback-style code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling return after await in express results in a hung request

emostar opened this issue · comments

Using express as a web server I am using tamejs, but have found that doing a return res.send(404); causes the request to get hung and never end.

If instead I do the below, it works as expected.

res.send(404);
return;

Is this a known limitation of tamejs?

You can't return values from a tamed function. The reason is that the function effectively "returns" to its caller after the first await statement. So if you have something like this:

function foo (cb) {
   var x = 1;
   await setTimeout (defer (), 100);
   x = 2
   return x;
}
var val = foo ();

It's not going to behave as you want. The return to the caller of foo is immediate, so if foo were to return any value, it would return 1. To skirt this confusion, tamed function just shouldn't return values. return within an tamed function can short-circuit out of the function, as normal.

But you're right, it's probably the right thing to do to evaluate the argument to return for its side effects, even though
the return value will be thrown away. I'll try to get that fix in today.

Ok, should be fixed in git and also npm (v0.3.5). Thanks for the bug report.

Not sure if this is the same problem or not, but this code doesn't work either:

function foo (cb) {
   var x = 1;
   return x;
   await setTimeout (defer (), 100);
}
var val = foo ();

val is undefined, but one would expect it to be 1.

I guess the whole function is a "tamed" function, so return won't work even before an await?