Ответ 1
Ваш код эквивалентен:
exports.someEndpoint = function(req, res) {
return request.post({
url: //etc
})
.then(function(response) {
setTimeout(function() {
res.status(200).json(response);
}, 2000);
});
};
Но только потому, что это обработчик экспресс-маршрута, который не должен возвращать что-либо вообще или обещание в частности.
С другой стороны, ваш код:
exports.someEndpoint = function(req, res) {
return request.post({
url: //etc
})
.then(function(response) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
// is this right?
resolve(
res.status(200).json(response);
);
}, 2000);
});
});
};
может быть вызван как:
yourModule.someEndpoint(req, res).then(function () {
// code to run after the timeout
});
что было бы невозможно в более короткой версии.