Skip to content

Instantly share code, notes, and snippets.

@shierro
Last active August 13, 2018 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shierro/d14b1de029237ae2a67265439e930a70 to your computer and use it in GitHub Desktop.
Save shierro/d14b1de029237ae2a67265439e930a70 to your computer and use it in GitHub Desktop.
promiseAll.js
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
}
}
})
});
}
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)
])
}
(async () => {
try {
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)
})()
@nimatrazmjo
Copy link

So you mean Request.updateMany does not need then and catch. it already returns promise.

@shierro
Copy link
Author

shierro commented Aug 13, 2018

If it's a mongodb call I suspect yes. you should check the official docs what updateMany returns but i suspect it's a promise. @nimatullah

@shierro
Copy link
Author

shierro commented Aug 13, 2018

@nimatullah updateMany only returns a callback, and not a promise, then you can use http://bluebirdjs.com/docs/api/promise.promisifyall.html to make Request functions return a promise

@nimatrazmjo
Copy link

I will test and I will get back to you.

@nimatrazmjo
Copy link

@shierro, could you please test my gist. I have done as you said but I need you to check the nested Promise.all of the main function. thanks in advance brother.
https://gist.github.com/nimatullah/ead58b9ce7081a641f69f7366406b317

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment