取消嵌套承诺我刚开始使用NodeJS中的请求和承诺编写网络代码。我希望删除这些嵌套的承诺,并将它们链锁起来,但是我不确定我将如何实现它/这是否是正确的方法。exports.viewFile = function(req, res) {var fileId = req.params.id;boxContentRequest('files/' + fileId + '/content', req.user.box.accessToken)
.then(function(response) {
boxViewerRequest('documents', {url: response.request.href}, 'POST')
.then(function(response) {
boxViewerRequest('sessions', {document_id: response.body.id}, 'POST')
.then(function(response) {
console.log(response);
});
});
});};这是请求代码:var baseContentURL = 'https://api.box.com/2.0/';var baseViewerURL = '
function boxContentRequest(url, accessToken) {
return new Promise(function (resolve, reject) {
var options = {
url: baseContentURL + url,
headers: {
Authorization: 'Bearer ' + accessToken,
}
};
request(options, function (err, res) {
if (err) {
return reject(err);
} else if (res.statusCode !== 200) {
err = new Error("Unexpected status code: " + res.statusCode);
err.res = res;
return reject(err);
}
resolve(res);
});
});}function boxViewerRequest(url, body, method) {
});
});}任何见解都将不胜感激。
添加回答
举报
0/150
提交
取消