asyncHandler

JSFunction asyncHandler(

  1. AsyncRequestHandler handler
)

Wraps an async handler for Express.js compatibility.

Express expects synchronous handlers, so this wrapper catches any errors and passes them to Express's error handling.

Implementation

JSFunction asyncHandler(AsyncRequestHandler handler) =>
    ((Request req, Response res, JSNextFunction next) {
      handler(req, res).catchError((Object error, StackTrace stack) {
        // Pass error to Express error handler
        next.callAsFunction(null, error.jsify());
      });
    }).toJS;