Created
August 29, 2016 22:18
-
-
Save ben-bradley/986a642895e98b331f8c58ab25257b3e to your computer and use it in GitHub Desktop.
A way to dynamically assign property names during an object declaration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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