Last active
September 9, 2017 19:21
-
-
Save blatayue/2417e54503a3f1d4db8be2da304378c0 to your computer and use it in GitHub Desktop.
Some practices I use often
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
| import someApi from 'some-api' | |
| async function query(queryString) { | |
| let results = await someApi.query(queryString); | |
| return results; | |
| }; | |
| query('string').then(results => { | |
| // do whatever here instead of in the function | |
| // or use another async function that doesn't return a promise | |
| }); | |
| // like this | |
| async function noThenHere() { | |
| let results = await query('string'); | |
| return results.pageOne; | |
| } | |
| console.log(noThenHere()); // Page one of results |
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
| let database = mongoose.connect('mongo://someDatabaseUri'); | |
| //ES6 arrow functions return implicitly if you don't add curly braces | |
| let curry = db => query => db.find(query); | |
| let spotifyApi = curry(database); | |
| // this db ORM is syncronous for this example | |
| let results = database.query('someString'); | |
| console.log(results) | |
| //{'id':123, 'name': 'example'} | |
| // Might seem like wizardry, but the previous function is identical to | |
| let curry = function(db) { | |
| return function(query) { | |
| return db.find(query) | |
| } | |
| } |
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
| let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 }; | |
| console.log(x); // 1 | |
| console.log(y); // 2 | |
| console.log(z); // { a: 3, b: 4 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment