You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So. I'm experiencing some quirks when attempting to implement some basic middleware for a Restify.js application I'm building with specific regard to next() and promise callbacks.
To express the problem in a generic form:
var server = restify.createServer({
name: config.name
});
Promise resolves:
server.use(function checkAcl(req, res, next) {
// Promise is resolved
var promise = function () {
var deferred = require('q').defer();
deferred.resolve();
return deferred.promise;
}
promise()
.then(function () {
next(); // doesn't get 'called', no response sent, connection eventually times out
}, function () {
res.send(new restify.NotAuthorizedError());
});
});
server.use(restify.bodyParser());
...
Promise is rejected
server.use(function checkAcl(req, res, next) {
// Promise is rejected
var promise = function () {
var deferred = require('q').defer();
deferred.reject();
return deferred.promise;
}
promise()
.then(function () {
next();
}, function () {
res.send(new restify.NotAuthorizedError()); // this works fine
});
}
});
server.use(restify.bodyParser());
...
Am I doing anything obviously wrong? Any insight? It certainly seems to been related to the promise callbacks, are they somehow suppressing the call to next()? Is this a bug?
The text was updated successfully, but these errors were encountered:
Follow up. I finally got to the bottom of this. It's down to the order in which middleware is added to the stack with specific regard to the bodyParser(). Adding it prior to the custom middleware produces the expected behaviour.
e.g.
Adding the restify.bodyParser() middleware BEFORE the custom middleware resolves the issue.
n.b:
// Add bodyParser() to the stack before the custom middleware
server.use(restify.bodyParser());
server.use(function (req, res, next) {
// Promise is resolved
var promise = function () {
var deferred = require('q').defer();
deferred.resolve();
return deferred.promise;
}
promise()
.then(function () {
next(); // next() now behaves as expected.
}, function () {
res.send(new restify.NotAuthorizedError());
});
});
...
So. I'm experiencing some quirks when attempting to implement some basic middleware for a Restify.js application I'm building with specific regard to
next()
and promise callbacks.To express the problem in a generic form:
Promise resolves:
Promise is rejected
Am I doing anything obviously wrong? Any insight? It certainly seems to been related to the promise callbacks, are they somehow suppressing the call to
next()
? Is this a bug?The text was updated successfully, but these errors were encountered: