您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

JavaScript删除嵌套的承诺

JavaScript删除嵌套的承诺

在每个then回调中,您都需要 返回 新的Promise:

exports.viewFile = function(req, res) {
    var fileId = req.params.id;
    @R_903_2419@ContentRequest('files/' + fileId + '/content', req.user.@R_903_2419@.accessToken)
      .then(function(response) {
          return @R_903_2419@ViewerRequest('documents', {url: response.request.href}, 'POST');
      })
      .then(function(response) {
          return @R_903_2419@ViewerRequest('sessions', {document_id: response.body.id}, 'POST');
      })
      .then(function(response) {
          console.log(response);
      });
};

.then()调用返回的承诺然后将使用“内部”承诺中的值进行解析,以便您可以轻松地链接它们。

通用模式:

somePromise.then(function(r1) {
    return nextPromise.then(function(r2) {
        return anyValue;
    });
}) // resolves with anyValue

     ||
    \||/
     \/

somePromise.then(function(r1) {
    return nextPromise;
}).then(function(r2) {
    return anyValue;
}) // resolves with anyValue as well
javascript 2022/1/1 18:13:49 有542人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶