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 / promisesMain.js
Created March 27, 2017 04:13
"main" code that wraps all the individual promise steps together
// requires https://gist.github.com/toddzebert/135578e9faf006b73656c360b7e616e2
// requires https://gist.github.com/toddzebert/c9d75cd901897e7363bf142a90a2b2a1
// requires https://gist.github.com/toddzebert/bcd849a48676256eef8a795f8f03cfff
// requires https://gist.github.com/toddzebert/43c308b0e0f559b47ac487dcc8c06924
// requires https://gist.github.com/toddzebert/5d279f8a2104bef103273e5177ae3599
var data = { id: 4 };
getAuth(data)
.then(getProfile)
.then(getFeed)
.then(getPosts)
@toddzebert
toddzebert / getAds.js
Created March 27, 2017 04:08
getAds code for use with getAd.js, and simAsync.js
// requires https://gist.github.com/toddzebert/996e297c1c081d19378eaf5f29dc75bb
// requires https://gist.github.com/toddzebert/ec087188b813e6490abab2cf0feb2ad5
var getAds = function (data) {
return new Promise( (resolve, reject) => {
Promise.race([getAd(data), getAd(data), getAd(data)])
.then(result => {
data.ads = result; resolve(data);
})
.catch(result => {
data.ads = result; reject(data);
@toddzebert
toddzebert / getAd.js
Created March 27, 2017 04:06
getAd code for use with simAsync.js
// requires https://gist.github.com/toddzebert/996e297c1c081d19378eaf5f29dc75bb
var getAd = (function (dat) {
let i = 1;
// create a closure
const ad = function () {
return 'Ad#' + i++;
};
return function (dat) {
return simAsync(ad);
};
@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 => {
@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 / 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 / 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 / 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 / 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 / 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.