News for hackers. Stuff that matters.
Re:Fixing the callback spaghetti in node.js by tilgovi on Thursday September 29, 2011 @03:15AM (#3051477)
Not necessarily. A callback function with the standard signature under the hood is implied. If the author is controlling flow into and out of these execution contexts there is no reason an error passed to the implicit callback cannot cause an exception to be thrown into the original execution. Throwing exceptions into coroutines is a fairly normal pattern.
Re:Fixing the callback spaghetti in node.js by tlrobinson on Thursday September 29, 2011 @03:19AM (#3051490)
True, but the existing Node APIs don't do that, they return the error as an argument to the callback.
Re:Fixing the callback spaghetti in node.js by snprbob86 on Thursday September 29, 2011 @04:02AM (#3051572)
The wiki page says:

    // these two lines are equivalent
    await a, b, c, d = moo(1, 2, 3);
    moo(1, 2, 3, function(a, b, c, d) {} );
So I don't see why not...

    // ...these two lines would be also equivalent
    await error, foo = bar(1, 2, 3);
    bar(1, 2, 3, function(error, foo) {} );
Re:Fixing the callback spaghetti in node.js by tlrobinson on Thursday September 29, 2011 @04:14AM (#3051598)
Exactly. The error object is a return value of the "await"-ed function, not an exception that's thrown.

Here's a concrete example. Normally in Node you do something like this:

  fs.readFile('/etc/passwd', function (err, data) {
    if (err) {
      // handle error
    }
    // normal processing
  });
The await version would look like this:

  await err, data = fs.readFile('/etc/passwd');
  if (err) {
    // handle error
  }
  // normal processing
Ideally it would look like this:

  try {
    await data = fs.readFile('/etc/passwd');
    // normal processing
  } catch (error) {
    // handle error
  }
Re:Fixing the callback spaghetti in node.js by koush on Thursday September 29, 2011 @04:34AM (#3051625)
This is certainly something that is doable and crossed my mind, but I did not want to have the await be restricted to functions that conform to the typical return arguments. For now at least.
Re:Fixing the callback spaghetti in node.js by dbrock on Thursday September 29, 2011 @02:42PM (#3053701)
How about

  try await foo = bar(baz)
as shorthand for

  await error, foo = bar(baz)
  if (error) throw error