Skip to content

Instantly share code, notes, and snippets.

View toddzebert's full-sized avatar
💭
coding, of course

Todd Zebert toddzebert

💭
coding, of course
View GitHub Profile
@toddzebert
toddzebert / JsArrowFunctions.md
Last active March 20, 2017 06:38
Just a quick intro to JavaScript Arrow Functions to be used as an "aside" in a medium post

Arrow Functions aka => aka Fat Arrow aka Lambdas

It's a sugary anon function with some key differences. (params) => { code }

Syntax

  • No need for the function keyword
  • No need for {} if the function is only a single line
  • Single arguments (except rest args) don't need () but 0 args do
  • return is implicit with single line functions
@toddzebert
toddzebert / flattenedAsyncCallbackAllRaceExample.js
Last active March 8, 2017 05:45
Flattened Async Callback example w/"all" & "race" patterns - check the console log
// requires https://gist.github.com/toddzebert/d41f06a7a33f5ada1557637a89278531
// requires https://gist.github.com/toddzebert/5958be630e81bb9ba16fb6931c339c88
var posts = [], data = { id: 4 };
var resolveAd = function(res) {
data.ad = res;
console.log(data);
}
var resolvePosts = function(res) {
@toddzebert
toddzebert / nestedAsyncCallbackAllRaceExample.js
Last active March 8, 2017 05:02
Nested Async Callback example w/"all" & "race" patterns - check the console log
// requires https://gist.github.com/toddzebert/d41f06a7a33f5ada1557637a89278531
// requires https://gist.github.com/toddzebert/5958be630e81bb9ba16fb6931c339c88
var posts = [], data = { id: 4 };
getAuth(data, function (res) {
data.auth = res;
getProfile(data, function (res) {
data.profile = res;
getFeed(data, function (res) {
data.feed = res;
var getPosts = data.feed.map(post => getPost(post));
@toddzebert
toddzebert / callbackRace.js
Last active March 8, 2017 04:23
A simple implementation of a "race" callback pattern such that the first one of an array `entries` callbacks to either resolve or reject determines the overall result
var once = function (func) {
var done = false;
return function (res) {
if (done) return;
if (res) {
func.apply(this, arguments);
} else {
func.apply(this, false);
}
done = true;
@toddzebert
toddzebert / asyncFunctionsAllRace.js
Last active March 8, 2017 04:10
"async" partial application functions for callback w/"all" & "race" examples
// requires https://gist.github.com/toddzebert/95bcc9b50febad0bb9680d3eab897797
// requires https://gist.github.com/toddzebert/b4465de41b0f8d317d7a685604983c36
var getAuth = asyncSim.bind(null, 'Authorized');
var getProfile = asyncSim.bind(null, 'Profile');
var getFeed = asyncSim.bind(null, function(data) {
for (var a = [], i = randInt(4, 2); i; i--) {
a.push(randInt(90, 1));
}
return a;
});
@toddzebert
toddzebert / nestedAsyncCallbackAllExample.js
Last active March 8, 2017 05:01
Nested Async Callback example w/"all" pattern - check the console log
// requires https://gist.github.com/toddzebert/7c0aa1f1ee3be2d39d301bd875fead25
// requires https://gist.github.com/toddzebert/ef155ae58efb5a3788a7b143b0f7d215
var posts = [], data = { id: 4 };
getAuth(data, function (res) {
data.auth = res;
getProfile(data, function (res) {
data.profile = res;
getFeed(data, function (res) {
data.feed = res;
var getPosts = data.feed.map(post => getPost(post));
@toddzebert
toddzebert / callbackAll.js
Last active March 7, 2017 09:33
a simple implementation of a "all" callback pattern such that all the callback functions in the `entries` array complete successfully before progressing
var all = function (entries, cb) {
var finishers = entries.length;
var allCB = (function(cb) {
return function (res) {
if (res) {
if (--finishers === 0) cb(res);
} else {
cb(false);
}
@toddzebert
toddzebert / asyncFunctionsAll.js
Last active March 7, 2017 09:25
"async" partial application functions for callback w/"all" example
// requires https://gist.github.com/toddzebert/95bcc9b50febad0bb9680d3eab897797
// requires https://gist.github.com/toddzebert/b4465de41b0f8d317d7a685604983c36
var getAuth = asyncSim.bind(null, 'Authorized');
var getProfile = asyncSim.bind(null, 'Profile');
var getFeed = asyncSim.bind(null, function(data) {
for (var a = [], i = randInt(4, 2); i; i--) {
a.push(randInt(90, 1));
}
return a;
});
@toddzebert
toddzebert / nestedAsyncCallbackExample.js
Last active March 7, 2017 08:45
Nested Async Callback example - check the console log
// requires https://gist.github.com/toddzebert/ca9b735f1fbcd627ddfead4a87378adb
var data = { id: 4 };
getAuth(data, function (res) {
data.auth = res;
getProfile(data, function (res) {
data.profile = res;
getFeed(data, function (res) {
data.feed = res;
console.log(data); // callback hell
});
@toddzebert
toddzebert / asyncFunctions.js
Last active March 7, 2017 07:34
"async" partial application functions for callback example; see https://gist.github.com/toddzebert/cee6d8b0475275be0f48d7dfef142bd3
// requires https://gist.github.com/toddzebert/95bcc9b50febad0bb9680d3eab897797
// requires https://gist.github.com/toddzebert/b4465de41b0f8d317d7a685604983c36
var getAuth = asyncSim.bind(null, 'Authorized');
var getProfile = asyncSim.bind(null, 'Profile');
var getFeed = asyncSim.bind(null, function(data) {
for (var a = [], i = randInt(4, 2); i; i--) {
a.push(randInt(90, 1));
}
return a;
});