Skip to content

Instantly share code, notes, and snippets.

@ktilcu
Last active October 28, 2017 19:31
Show Gist options
  • Select an option

  • Save ktilcu/f9d528751aee54bfe40f09e111e5abcd to your computer and use it in GitHub Desktop.

Select an option

Save ktilcu/f9d528751aee54bfe40f09e111e5abcd to your computer and use it in GitHub Desktop.
// Imperative A/A
const makeRequest = async () => {
const data = await getJSON()
if (data.needsAnotherRequest) {
const moreData = await makeAnotherRequest(data);
console.log(moreData)
return data.concat(moreData); // added this
} else {
console.log(data)
return data
}
}
// Promises
const another = (data) => makeAnotherRequest(data).then(more=>data.concat(more));
const makeRequest = () => getJSON()
.then(data => data.needsAnotherRequest ? another(data) : data)
// Promises and Ramda
const another = (data) => makeAnotherRequest(data).then(R.concat(data));
const makeRequest = () => getJSON()
.then(
R.ifElse(
R.prop('needsAnotherRequest'), // if
another, // then
R.identity // else
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment