Created
September 25, 2017 16:55
-
-
Save Swivelgames/630ca47ec20943f8a9a9e7761ecff972 to your computer and use it in GitHub Desktop.
Obj Flatten, then Reduce Promises
This file contains hidden or 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
| const obj = { "foo": { "bar": "0", "pew": "hell world" }, "quux": { "baz": "1", "world": "wonderful" }}; | |
| const flatObj = Object.keys(obj).reduce((retObj, key) => Object.assign({}, | |
| retObj, | |
| Object.keys(obj[key]).reduce((deepRetObj, deepKey) => Object.assign({}, | |
| deepRetObj, | |
| { [`${key}.${deepKey}`]: obj[key][deepKey] } | |
| ), {}) | |
| ), {}); | |
| console.log(JSON.stringify(flatObj)); | |
| Object.keys(flatObj).reduce((prevProm, key) => ( | |
| prevProm.then((prevVal) => | |
| new Promise( | |
| resolve => { setTimeout(() => { console.log(key); resolve(key); }, 1000); } | |
| ) | |
| ) | |
| ), Promise.resolve()).then( | |
| (output) => { console.info(JSON.stringify(output)); }, | |
| (errors) => { console.error(JSON.stringify(errors)); } | |
| ); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: