Ali Razmjo
Ali Razmjo

Reputation: 73

Promise.all response

Can I add Promise.resolve(value) and Promise.reject(error) at response of Promise.all().

For example,

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));
  });
}

I don't know whether is it correct or not, but what I need is to handle the response of transferRFQ properly because I will need to add transferRFQ in another Promise.all() to handle the error properly.

Am I doing in the wrong way? if so, How to do it correctly

Any additional advice is welcome!

Upvotes: 1

Views: 106

Answers (1)

Theo
Theo

Reputation: 2042

I advice that you should not use unnecessary Promise wrappers, & avoid javascript hoisting.

You should try to do something like this instead

// 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)
})()

Upvotes: 1

Related Questions