Skip to content

Instantly share code, notes, and snippets.

@blatayue
Last active September 9, 2017 19:21
Show Gist options
  • Select an option

  • Save blatayue/2417e54503a3f1d4db8be2da304378c0 to your computer and use it in GitHub Desktop.

Select an option

Save blatayue/2417e54503a3f1d4db8be2da304378c0 to your computer and use it in GitHub Desktop.
Some practices I use often
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
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)
}
}
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