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 / bpExport.demo.html
Created February 10, 2017 06:11
Sharing CSS breakpoints with JavaScript - Demo HTML
site breakpoint: <span class="my-site-breakpoint"></span>
<div class="my-div">
my div breakpoint: <span class="my-div-breakpoint"></span>
</div>
<div class="my-color-div"></div>
@toddzebert
toddzebert / randInt.js
Last active March 7, 2017 08:25
a helper utility that returns a random integer; see https://gist.github.com/toddzebert/cee6d8b0475275be0f48d7dfef142bd3
function randInt (spread, base) {
return Math.floor(Math.random() * spread + (isNaN(base) ? 0: base));
}
@toddzebert
toddzebert / asyncSim.js
Last active March 7, 2017 08:25
"simulates" an async function and passes either `res` directly or the result of `res()` to the callback function, see https://gist.github.com/toddzebert/cee6d8b0475275be0f48d7dfef142bd3
// requires https://gist.github.com/toddzebert/b4465de41b0f8d317d7a685604983c36
function asyncSim (res, data, onDone) {
var result = (typeof res === "function") ? res(data) : res;
setTimeout(function () { onDone(result); }
, randInt(400, 200));
};
@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;
});
@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 / 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 / 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 / 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 / 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 / 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;