Last active
August 13, 2018 11:11
-
-
Save shierro/d14b1de029237ae2a67265439e930a70 to your computer and use it in GitHub Desktop.
promiseAll.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
})() |
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
@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
I will test and I will get back to you.
@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
So you mean
Request.updateMany
does not needthen
andcatch
. it already returns promise.