Skip to content

Instantly share code, notes, and snippets.

@ben-bradley
Created August 29, 2016 22:18
Show Gist options
  • Save ben-bradley/986a642895e98b331f8c58ab25257b3e to your computer and use it in GitHub Desktop.
Save ben-bradley/986a642895e98b331f8c58ab25257b3e to your computer and use it in GitHub Desktop.
A way to dynamically assign property names during an object declaration
'use strict';
/**
* With a Promise that receives a context object, but returns a different value that you want to
* have assigned to the context, you can assign dynamic property names like this:
*/
const keep = (ctx, prop) => (value) => Object.assign(ctx, { [ prop ]: value });
const session = {};
Promise.resolve(session)
.then((session) => Promise.resolve('bar').then(keep(session, 'foo')))
.then((session) => console.log(session)) // { foo: 'bar' }
.catch((err) => console.log('uhoh', err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment