Skip to content

Instantly share code, notes, and snippets.

@jcuffe
jcuffe / retries.js
Created April 12, 2019 17:42
Promise retry
// Common pattern of wrapping a library async function in a promise
function retriesAsyncOperation (args, retriesLeft = 5) {
return new Promise((resolve, reject) => {
performAsyncOp(args)
.then(result => resolve(result)
.catch((error) => {
if (retriesLeft == 0) {
reject(error)
} else {
// Passing reject as second argument allows outermost promise to fail when retries run out
@jcuffe
jcuffe / git.diff
Created February 13, 2019 05:10
Diffs for bugfixes
diff --git a/backend/package.json b/backend/package.json
index d0b2711..b731869 100644
--- a/backend/package.json
+++ b/backend/package.json
@@ -16,6 +16,7 @@
"author": "Vincent Ning",
"license": "ISC",
"dependencies": {
+ "cors": "^2.8.5",
"express": "^4.16.4",
@jcuffe
jcuffe / js
Created January 7, 2019 19:49
$(document).ready(function(e){
//commID = getCommunity();
commID = "ACH";
var campusArray = ilHeadshots.filter(function(item){
return item.community == commID;
});
console.log(ilHeadshots);
console.log("");
console.log(campusArray);
import React from "react";
import axios from "axios";
import Export from "./Export";
import Login from "./Login";
class App extends React.Component {
state = {
user: null
};
@jcuffe
jcuffe / server.js
Created November 21, 2018 20:45
axios caching
//
// Cache the response of an axios request
//
const cacheRequest = request => {
let cache = null;
return () => {
cache = cache || request.then(({ data }) => data);
return cache;
};
@jcuffe
jcuffe / market.js
Created October 25, 2018 04:35
Making 1000 requests without angering the remote server
const ordersEpic = (action$, state$) => action$.pipe(
// Listen for this action
ofType(type.GET_ORDERS),
// Replace the action with the list of active IDs
mapTo(state$.value.market.ids),
// Turn the list into a stream of ids
flatMap(from),
@jcuffe
jcuffe / server.js
Created October 17, 2018 00:31
ESI intermediate server to transform data into usable format for the client
const express = require('express');
const axios = require('axios');
const endpoints = require('./endpoints');
const knex = require('knex')({
client: 'sqlite3',
connection: {
filename: "./sde.sqlite"
}
});
diff --git a/hash-tables/ex1/ex1.py b/hash-tables/ex1/ex1.py
index 3039cf3..e7e289d 100644
--- a/hash-tables/ex1/ex1.py
+++ b/hash-tables/ex1/ex1.py
@@ -1,14 +1,14 @@
def get_indices_of_item_weights(weights, limit):
- hash_table = { i : weights[i] for i in range(0, len(weights) ) }
- hash_table2 = {y:x for x,y in hash_table.items()}
+ hash_table = { weights[i] : i for i in range(len(weights)) }
diff --git a/hash-tables/ex1/ex1.py b/hash-tables/ex1/ex1.py
index 3039cf3..e7e289d 100644
--- a/hash-tables/ex1/ex1.py
+++ b/hash-tables/ex1/ex1.py
@@ -1,14 +1,14 @@
def get_indices_of_item_weights(weights, limit):
- hash_table = { i : weights[i] for i in range(0, len(weights) ) }
- hash_table2 = {y:x for x,y in hash_table.items()}
+ hash_table = { weights[i] : i for i in range(len(weights)) }
@jcuffe
jcuffe / graphql>schema.js
Last active August 25, 2018 21:15
Apollo server and client together
const typeDefs = `
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => "Hello, world!"
}