Skip to content

Instantly share code, notes, and snippets.

@jsmanifest
Created June 22, 2019 04:29
Show Gist options
  • Save jsmanifest/0a1e783a197b793bd0dcd1e8ff40c8d2 to your computer and use it in GitHub Desktop.
Save jsmanifest/0a1e783a197b793bd0dcd1e8ff40c8d2 to your computer and use it in GitHub Desktop.
const preparePeopleWithFavoriteColor = (color) => {
const _people = []
return {
getPeople() {
return _people
},
addPeople(people) {
_people.push(...people)
},
addPerson(person) {
_people.push(person)
},
gather(people) {
if (Array.isArray(people)) {
people.forEach((person) => {
if (color === person.favoriteColor) {
_people.push(person)
}
})
}
},
}
}
const peopleWhoLoveRed = preparePeopleWithFavoriteColor('red')
axios
.get('https://someapi.com/peoples')
.then((response) => {
const people = response.data.result
if (people.length) {
peopleWhoLoveRed.gather(people)
}
return axios
.get('https://someapi.com/other-peoples')
.then((response) => {
const morePeople = response.data.result
if (morePeople.length) {
everyoneWhoLovesRed.gather(morePeople)
}
return
})
.then(() => {
// finally, add me last because i love red too
peopleWhoLoveRed.addPerson({
nickName: 'jsmanifest',
favoriteColor: 'red',
})
return axios.post('https://api.redlovers.com/v1/subscribers/', {
people: peopleWhoLoveRed.getPeople(),
})
})
})
.catch((error) => {
console.error(error)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment