Skip to content

Instantly share code, notes, and snippets.

@Avaq
Last active Oct 12, 2021
Embed
What would you like to do?
Dangerous Promises
//
// utils
//
const thrush = x => f => f(x);
const indexBy = k => xs => xs.reduce((index, x) => {
if(index[x[k]] != null) index[x[k]].push(x);
else index[x[k]] = [x];
return index;
}, {});
const mapObject = f => o => Object.keys(o).reduce((mapped, k) => {
mapped[k] = f(o[k]);
return mapped;
}, {});
//
// program
//
const fetchUsers = _ => Promise.resolve([
{id: 1, name: 'bob'},
{id: 2, name: 'frank'},
{id: 3, name: 'bob'},
]);
const isLongerThan = xs => n => xs.length > n;
fetchUsers()
.then(indexBy('name'))
.then(mapObject(isLongerThan))
.then(mapObject(thrush(1)))
.then(console.log, console.error);
@Avaq
Copy link
Author

Avaq commented Aug 22, 2017

This somewhat-contrived-but-not-unthinkable program loads a list of users and writes to the terminal, for every user name, whether there exist more than one users with this name.

Now what happens when one of the users names themselves then?

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