Skip to content

Instantly share code, notes, and snippets.

@edwardgalligan
Created July 24, 2019 13:39
Show Gist options
  • Save edwardgalligan/1a2690246725a71fb627b8d209d1c1ca to your computer and use it in GitHub Desktop.
Save edwardgalligan/1a2690246725a71fb627b8d209d1c1ca to your computer and use it in GitHub Desktop.
What not to do
const resolveObjectPromises = async a => (async b => await Promise.all(Object.keys(a).map(async k => b[k] = await a[k])) && b)({});
@edwardgalligan
Copy link
Author

edwardgalligan commented Jul 24, 2019

This is an example of a solution to a problem that need not exist.

i.e. a concrete argument for structuring data a certain way, and not structuring data a certain way.

In general, structuring data in a way that is easy to parse is usually the same as structuring data that is easy to create/read/handle.

In this case, the source object is of the form:

const sourceObject = {
  prop1: Promise.resolve(a),
  prop2: Promise.resolve(b)
};

This was probably created with specific use-cases in mind looking like:

// in one file
const x = await sourceObject.prop1;

// in an entirely separate file
const y = await sourceObjet.prop2;

if this is intended to be your only use-case, these two props shouldn't be on the same object. If you're intending the object to be handled somewhere in code as a single entity, it should look more like:

const sourceObject = Promise.resolve({
  prop1: a,
  prop2: b
});

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