Skip to content

Instantly share code, notes, and snippets.

Alex Girard grabcode

View GitHub Profile
@grabcode
grabcode / intervalAsync.js
Created Jul 26, 2017
Utility function to cope with `interval` executing asynchronous call.
View intervalAsync.js
/*
`setInterval` triggers a function every X secs.
It's not satifying when you deal with asynchronous functions, such as API calls.
It is indeed possible that the response comes right before the next scheduled call, or even after, defeating the purpose of intervals.
The `interval` function below aims at solving this issue.
Usage:
// real life usage, used in a redux thunk
interval(() => bodymap.get(), {
@grabcode
grabcode / seasoning.js
Created Jul 24, 2017
Is winter coming? `seasoning.js` adds season awareness to your programs.
View seasoning.js
/*
`seasoning.js` adds season awareness to your programs. Help finding out what season it is.
Usage:
// Get the season using the user's browser location (NOTE: it requires the user's authorisation)
seasoning.getSeason()
.then(console.log);
// Get the season using the user's geoip
seasoning.getSeason({ locationProvider: 'ipinfo' })
@grabcode
grabcode / ES6-promise-resolveAll.js
Last active Jul 5, 2017
resolveAll is concurrently running promises until they all either resolve or reject. Free to customise how to handle failure.
View ES6-promise-resolveAll.js
/*
2017, still not using Promises? You missed the memo.
Promise.all lets you run a serie (iteratable list) of promises but will reject as soon as one reject.
Sometimes though, you want to try resolving all of them, and then address the rejected ones. I call that "gracefully fail".
Usage (promises is an array of promise):
// Using the default error handler
resolveAll(promises)
.then(resolved => console.log('List of my resolved promises result'))
.catch(errors => {
@grabcode
grabcode / ES6-request-json.js
Last active Jul 5, 2017
ES6 fetch syntactic sugar: handling common fetch usage with json content type, same headers, and stringified body
View ES6-request-json.js
/*
fetch is awesome, there's no question about it.
But aren't you tired of:
- Writing your `res => res.json()` handler
- Having to stringify your body
- Signin your request with the same headers (```{'Content-Type': json, Authorization: 'JWT ...'}```)
- Inconsistently handling the response status code and not reject promise when relevant.
Usage:
request('http://yourawesome.api.com').then(console.log).catch(console.error);
View AWS Serverless Architecture notes.txt
Using native nodejs packages?
https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/
How to figure out the amount of memory needed?
https://github.com/berezovskyi/lambda-test
How does my lambda consume external APIs?
https://aws.amazon.com/blogs/aws/aws-ip-ranges-json/
When a lambda fails, does it retry itself?
View cookie-recipe.html
@grabcode
grabcode / validCandidate.js
Last active Apr 12, 2016
validCandidate function
View validCandidate.js
function validCandidate(languages) {
let validCandidate = false
let languageRegex = /script$/
if(Array.isArray(languages)) {
validCandidate = !!languages.filter( (language)=> languageRegex.test(language) ).length
} else if (typeof languages == 'string') {
validCandidate = languageRegex.test(languages)
}
return validCandidate
}
@grabcode
grabcode / scope_explorer.js
Last active Feb 2, 2016
Explore or Spy the global variables available, or any given scope. By default, it run in the global browser scope `window`, and exclude its default properties.
View scope_explorer.js
/*
* Explore/Spy App Global variables, excluding defaults (defaults comes down a scope)
* > Run me in your dev tool console via copy/pasting
* > In return, I provide a list of keys, and copy in your clipboard (how sweet is that!)
*
* Follow my creator https://twitter.com/grabthecode
*/
;(function(scope, defaults){
@grabcode
grabcode / snippets.cson
Last active Dec 28, 2015
ReactNative Component and Styles Snippet
View snippets.cson
'.source.js':
'ReactNative Component':
'prefix': 'rnc'
'body': """
'use strict';
import React, {
Text,
} from 'react-native';
You can’t perform that action at this time.