Skip to content

Instantly share code, notes, and snippets.

View benmccormick's full-sized avatar

Ben McCormick benmccormick

View GitHub Profile

Keybase proof

I hereby claim:

  • I am benmccormick on github.
  • I am ben_mccormick (https://keybase.io/ben_mccormick) on keybase.
  • I have a public key ASA-PhwIqziP2g1Ycexv29-bEBSVn-qNjgtQRZ0ZLDjIbgo

To claim this, I am signing this object:

@benmccormick
benmccormick / mobx-codemod.js
Last active June 17, 2018 21:39
Mobx Codemod Example
module.exports = function (file, api) {
const j = api.jscodeshift;
const buildDecoratorPropertiesFromCurrentObject = currentObj => {
let decoratorProps = [];
currentObj.properties.forEach(prop => {
if (prop.value.type === 'CallExpression' && prop.value.callee.name === 'computed') {
prop.kind = 'get';
let fnBody = prop.value.arguments[0];
@benmccormick
benmccormick / projects.txt
Created June 11, 2018 03:31
Github Top 25 Libraries/Frameworks June 10th 2018
bootstrap -css
tensorflow - python
react -js
vue -js
d3 - js
react-native -js
angular.js - js
animate.css - css
jquery - js
laravel - php
@benmccormick
benmccormick / params.js
Created December 22, 2017 03:11
Params fn
/* takes a list of params in the format
[{
key: 'foo'
value: 'bar'
}]
or an object with key-value params
{
foo: 'bar'
}
}
@benmccormick
benmccormick / file.js
Created December 24, 2015 04:08
Array.prototype functional methods
let arr = [1, 2, 3];
function log(item) {
console.log(item);
}
//forEach performs an action on each item
//in an array
arr.forEach(log); //logs 1 2 3 in order
@benmccormick
benmccormick / file.js
Created December 24, 2015 03:41
Super Simple async-await example
async function showMessageFromServer() {
let data = await fetch('/get/data.json');
let message = data.json().message;
alert(message);
}
@benmccormick
benmccormick / file.js
Created December 24, 2015 03:26
Promise Examples
//Promises take a function that receives callbacks that can be run when an operation completes
let delay5Seconds = new Promise(function(resolve, reject) {
setTimeout(resolve, 5000);
});
//You can respond to the results of a successfully resolved Promise using the `then` function
delay5Seconds.then(function() {
console.log('This gets logged 5 seconds later')
});
@benmccormick
benmccormick / file.js
Created December 24, 2015 03:23
Waiting on multiple async operations using Promises
//fetch makes an HTTP request and returns a promise that resolves
//when the file is loaded
let p1 = fetch('/data/file1.json');
let p2 = fetch('/data/file2.json');
let p3 = fetch('/data/file3.json');
Promise.all([p1,p2,p3]).then(function(values) {
//values contains the responses from each of the promises
let [response1, response2, response3] = values;
@benmccormick
benmccormick / file.js
Created December 24, 2015 03:20
Converting callbacks to promises
//Backbone.Model's save function is a function that takes callbacks
//for successful or failed saves.
//Normal usage
let model = new Backbone.Model();
model.save(null, {
success: function() {
alert('saved');
@benmccormick
benmccormick / promises.js
Last active December 24, 2015 03:19
Promises swallow exceptions
try {
fetch('/some/data').then(function() {
throw 'error';
})
} catch(e) {
//this code won't be executed
console.log('Exception Caught')
}