所以。在尝试为我正在构建的 Restify.js 应用程序实现一些基本的中间件时,我遇到了一些怪癖,并特别关注next()
并承诺回调。
以通用形式表达问题:
var server = restify.createServer({
name: config.name
});
承诺解决:
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());
...
承诺被拒绝
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());
...
我在做任何明显错误的事情吗?有什么见解吗?这似乎与承诺回调有关,它们是否以某种方式抑制了对 的调用next()
?