无极回应

时间:2018-08-13 09:06:24

标签: javascript promise

我可以在响应Promise.resolve(value)时添加Promise.reject(error)Promise.all()

例如,

function transferFRQ(fromUserId, fromCompanyDetails, toUserDetails, toCompanyDetails,) {
  return Promise.all([
    transferRFQCompany(fromCompanyDetails, toCompanyDetails),
    replaceRFQCreatedBy(fromUserId, toUserDetails)
  ])
    .then(result => Promise.resolve(result))
    .catch(error => Promise.reject(error));
}

function transferRFQCompany (fromCompanyDetails, toCompanyDetails) {
  return new Promise((resolve, reject) => {
    Request.updateMany({
      "company.id": fromCompanyDetails._id
    }, {
      $set: {
        company: {
          id: toCompanyDetails._id,
          name: toCompanyDetails.name,
          logo: toCompanyDetails.logo
        }
      }
    }).then(result => resolve(result))
      .catch(error => reject(error));
  });
}

function replaceRFQCreatedBy (fromUserId, toUserDetails) {
  return new Promise((resolve, reject) => {
    Request.updateMany({
      "createdBy.id": fromUserId
    }, {
      $set: {
        createdBy: {
          id: toUserDetails._id,
          firstName: toUserDetails.firstMame,
          lastName: toUserDetails.lastName
        }
      }
    }).then(result => resolve(result))
      .catch(error => reject(error));
  });
}

我不知道它是否正确,但是我需要正确处理transferRFQ的响应,因为我需要在另一个transferRFQ中添加Promise.all()正确处理错误。

我做错了吗?如果是这样,如何正确执行

欢迎任何其他建议!

1 个答案:

答案 0 :(得分:1)

我建议您不要使用不必要的Promise包装器,并避免 javascript吊起

您应该尝试执行类似的操作

// Note that this is declared before used, to avoid javascript hoisting
function transferRFQCompany (fromCompanyDetails, toCompanyDetails) {
    return Request.updateMany({ // updateMany already returns a promise right? no need to wrap it in another promise
      "company.id": fromCompanyDetails._id
    }, {
      $set: {
        company: {
          id: toCompanyDetails._id,
          name: toCompanyDetails.name,
          logo: toCompanyDetails.logo
        }
      }
    })
  });
}

// Note that this is declared before used, to avoid javascript hoisting
function replaceRFQCreatedBy (fromUserId, toUserDetails) {
  return Request.updateMany({ // updateMany already returns a promise right? no need to wrap it in another promise
    "createdBy.id": fromUserId
  }, {
    $set: {
      createdBy: {
        id: toUserDetails._id,
        firstName: toUserDetails.firstMame,
        lastName: toUserDetails.lastName
      }
    }
  })
}

function transferFRQ(fromUserId, fromCompanyDetails, toUserDetails, toCompanyDetails,) {
  return Promise.all([
    transferRFQCompany(fromCompanyDetails, toCompanyDetails),
    replaceRFQCreatedBy(fromUserId, toUserDetails)
  ])
}
// Sample usage async/await style
(async () => {
  try {
      // put your params, of course
      const result = await transferFRQ(...params); 
      // `result` is result of .then()
  } catch (e) {
    // `e` is result of .catch()
  }

  // or use it in promise-style
  transferFRQ(...params)
    .then(console.log)
    .catch(console.error)
})()