取消嵌套承诺
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) { }); });}
相关分类