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 / 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 / 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 / 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 / JsIterables.md
Last active March 20, 2017 06:47
A quick overview of JavaScript Iterables

An Iterable is an object that has a method Symbol.iterator which is factory for Iterators. Native Iterables include which include Arrays, Stings, Set & Map (but not weak variants).

An Iterator includes a "pointer" for traversing an Iterable in the form of a next method.

@toddzebert
toddzebert / simAsync.js
Last active March 27, 2017 02:34
"simulates" an async function with Promises
// requires https://gist.github.com/toddzebert/b4465de41b0f8d317d7a685604983c36
var simAsync = function(fulfillment, rejection) {
return new Promise( (resolve, reject) => {
const delay = randInt(400, 200);
setTimeout(() => { resolve(fulfillment()); }, delay);
});
};
@toddzebert
toddzebert / getAuth.js
Created March 27, 2017 03:55
getAuth code for use with simAsync.js
// requires https://gist.github.com/toddzebert/996e297c1c081d19378eaf5f29dc75bb
var getAuth = function(data) {
// create a closure
const auth = function () {
data.auth = "Authorized"; return data;
};
return simAsync(auth);
};
@toddzebert
toddzebert / getProfile.js
Created March 27, 2017 03:57
getProfile code for use with simAsync.js
// requires https://gist.github.com/toddzebert/996e297c1c081d19378eaf5f29dc75bb
var getProfile = function(data) {
// create a closure
const prof = function () {
data.profile = "Profile"; return data;
};
return simAsync(prof);
};
@toddzebert
toddzebert / getFeed.js
Created March 27, 2017 03:59
getFeed code for use with simAsync.js
// requires https://gist.github.com/toddzebert/996e297c1c081d19378eaf5f29dc75bb
var getFeed = function(data) {
// create a closure
const feed = function () {
const a = [];
for (let i = randInt(4, 2); i; i--) {
a.push(randInt(90, 1));
}
data.feed = a;
return data;
@toddzebert
toddzebert / getPost.js
Created March 27, 2017 04:00
getPost code for use with simAsync.js
// requires https://gist.github.com/toddzebert/996e297c1c081d19378eaf5f29dc75bb
var getPost = function (data) {
// create a closure
const post = function () {
return data + 100;
};
return simAsync(post);
};
@toddzebert
toddzebert / getPosts.js
Last active March 27, 2017 04:08
getPosts code for use with getPost.js, and simAsync.js
// requires https://gist.github.com/toddzebert/996e297c1c081d19378eaf5f29dc75bb
// requires https://gist.github.com/toddzebert/9dc7de3ed0195dfefcc533bcba426930
var getPosts = function (data) {
return new Promise( (resolve, reject) => {
const getAllPosts = data.feed.map(post => getPost(post));
Promise.all(getAllPosts)
.then(result => {
data.posts = result; resolve(data);
})
.catch(result => {