Skip to content

Instantly share code, notes, and snippets.

samzhao

Block or report user

Report or block samzhao

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@samzhao
samzhao / index.js
Last active Jun 30, 2018
Usage of delayedMap helper
View index.js
const toListItem = async item => {
const { thing } = await sendRequest();
return thing;
};
const delayTime = 100;
const items = delayedMap(listOfStuff, toListItem, delayTime);
@samzhao
samzhao / index.js
Created Jun 30, 2018
Delayed map helper
View index.js
const delayedMap = async (list, iterator, delayTime) => {
let promises = [];
for (const item of list) {
await delay(delayTime);
promises.push(await iterator(item));
}
return Promise.all(promises);
};
@samzhao
samzhao / index.js
Created Jun 30, 2018
Making async/await delay helper work in a loop
View index.js
const delay = time =>
new Promise(resolve => setTimeout(resolve, time));
const list = new Array(10).fill();
for (let index of list.keys()) {
await delay(1000);
console.log(index);
}
@samzhao
samzhao / index.js
Created Jun 30, 2018
Bad example of using async/await for delaying calls in a loop
View index.js
const delay = time =>
new Promise(resolve => setTimeout(resolve, time));
const list = new Array(10).fill();
list.forEach(async (_, index) => {
await delay(1000);
console.log(index);
});
@samzhao
samzhao / index.js
Created Jun 30, 2018
Delay with async/await
View index.js
const delay = time =>
new Promise(resolve => setTimeout(resolve, time));
const sendRequest = async () => {
await delay(1000);
return axios.get(API_URL);
}
// request fires after 1 second
sendRequest();
@samzhao
samzhao / index.js
Last active Jun 30, 2018
Simple Async/await Example
View index.js
import axios from 'axios';
const API_URL = "https://jsonplaceholder.typicode.com/posts/1";
// Before async/await
const logPostId = () => {
axios.get(API_URL)
.then(resp => {
const { data } = resp;
const { id } = data;
@samzhao
samzhao / gist:1991822
Created Mar 7, 2012
Javascript: jQuery pubsub
View gist:1991822
(function($) {
var o = $( {} );
$.each({
on: 'subscribe',
trigger: 'publish',
off: 'unsubscribe'
}, function( key, api ) {
$[api] = function() {
o[key].apply( o, arguments );
View ui-bootstrap-tpls.min.js
angular.module("ui.bootstrap",["ui.bootstrap.tpls","ui.bootstrap.transition","ui.bootstrap.collapse","ui.bootstrap.accordion","ui.bootstrap.alert","ui.bootstrap.bindHtml","ui.bootstrap.buttons","ui.bootstrap.carousel","ui.bootstrap.position","ui.bootstrap.datepicker","ui.bootstrap.dropdownToggle","ui.bootstrap.modal","ui.bootstrap.pagination","ui.bootstrap.tooltip","ui.bootstrap.popover","ui.bootstrap.progressbar","ui.bootstrap.rating","ui.bootstrap.tabs","ui.bootstrap.timepicker","ui.bootstrap.typeahead"]),angular.module("ui.bootstrap.tpls",["template/accordion/accordion-group.html","template/accordion/accordion.html","template/alert/alert.html","template/carousel/carousel.html","template/carousel/slide.html","template/datepicker/datepicker.html","template/datepicker/popup.html","template/modal/backdrop.html","template/modal/window.html","template/pagination/pager.html","template/pagination/pagination.html","template/tooltip/tooltip-html-unsafe-popup.html","template/tooltip/tooltip-popup.html","template/popo
@samzhao
samzhao / 0_reuse_code.js
Created Oct 9, 2013
Here are some things you can do with Gists in GistBox.
View 0_reuse_code.js
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@samzhao
samzhao / Prettier Development
Created Oct 13, 2012
Gemfile Template with Wirble, Hirb, awesome_print, and annotate
View Prettier Development
gem('wirble', group: "development")
gem('hirb', group: "development")
gem('awesome_print', group: "development")
gem('annotate', group: "development")
You can’t perform that action at this time.