Skip to content

Instantly share code, notes, and snippets.

@ztolley
Created February 14, 2017 12:20
Show Gist options
  • Save ztolley/9f07d67fbbe52593684af281115eb6a7 to your computer and use it in GitHub Desktop.
Save ztolley/9f07d67fbbe52593684af281115eb6a7 to your computer and use it in GitHub Desktop.
Generator sync requests
function getDitCompany (id) {
return new Promise((resolve, reject) => {
Q.spawn(function *main () {
try {
const company = yield request(`${config.apiRoot}/company/${id}/`)
// get related information
const relatedKeys = Object.keys(relatedProperties)
for (const property of relatedKeys) {
if (company[property] && company[property].length > 0) {
const metadataKey = relatedProperties[property]
const values = metadataRepository[metadataKey]
company[property] = values.filter(item => item.id === company[property])[0]
}
}
// get related companies
const relatedCompanies = yield request(`${config.apiRoot}/company/${company.id}/related/`)
company.parents = []
for (const id of relatedCompanies.parents) {
const parent = yield request(`${config.apiRoot}/company/${id}/`)
company.parents.push(parent)
}
company.children = []
for (const id of relatedCompanies.children) {
const child = yield request(`${config.apiRoot}/company/${id}/`)
company.children.push(child)
}
if (company.company_number && company.company_number.length > 0) {
const ch = yield request(`${config.apiRoot}/ch-company/${company.company_number}/`)
company.companies_house_data = ch
}
resolve(company)
} catch (error) {
reject(error)
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment