Skip to content

Instantly share code, notes, and snippets.

View tmpaul06's full-sized avatar

Tharun M Paul tmpaul06

View GitHub Profile
@tmpaul06
tmpaul06 / Column.jsx
Last active August 29, 2015 14:23
Render a child with parent context info
import React from "react";
import BaseComponent from "../BaseComponent";
import DomEvents from "../utils/DomEvents";
export default class Column extends BaseComponent {
constructor() {
super();
this.state = {
parentWidth: null
};
this.mounted = false;
@tmpaul06
tmpaul06 / add.js
Created November 21, 2016 10:39
An example showing synchronous and asynchronous code execution
// A synchronous function.
// It returns a result as soon as it is called.
function syncAdd(a, b) {
return a + b;
}
// A function that adds two numbers after two seconds
function asyncAdd(a, b) {
setTimeout(function() {
return a + b;
@tmpaul06
tmpaul06 / mango_promise.js
Last active July 3, 2017 07:21
Example promise gist
promise
.then(buyMangoes, dadStrategy);
// No error. No warning. Error is simply gone.
@tmpaul06
tmpaul06 / promise_constructor.js
Created July 3, 2017 07:33
Promise constructor
function getSomeJSON(url, callback) {
// AJAX call to get data from server. Accepts callback and calls
// with callback(err, response)
...
}
function fetchJSON(url) {
return new Promise(function (resolve, reject) {
getSomeJSON(url, function (err, response) {
if (err) return reject(err);
@tmpaul06
tmpaul06 / promise_deferred.js
Created July 3, 2017 07:45
Promise deferred
function getSomeJSON(url, callback) {
// AJAX call to get data from server. Accepts callback and calls
// with callback(err, response)
...
}
function fetchJSON(url) {
var deferred = new Deferred();
getSomeJSON(url, function (err, response) {
if (err) return deferred.reject(err);
@tmpaul06
tmpaul06 / remote_promise_again.js
Last active July 3, 2017 07:53
Remote data promise
// The first call returns promise1
remoteDataCall().then(function (result) {});
// Second call returns another promise2 !== promise1
remoteDataCall().then(function (result) {});
@tmpaul06
tmpaul06 / remote_same.js
Created July 3, 2017 07:53
Remote data call cache
var promise = remoteDataCall();
promise.then(function (result) {});
promise.then(function (result) {});
// Same result as above without executing `remoteDataCall` again.
@tmpaul06
tmpaul06 / then.js
Created July 3, 2017 08:10
Promise chain
// domeSomethingAsync is the first promise.
var result = doSomethingAsync()
// It has a `then` method for chaining
.then(...)
// result is a different Promise. It has its own then method
result.then(...) => returns another new Promise
@tmpaul06
tmpaul06 / mango_promise_main.js
Last active July 6, 2017 07:10
Mango promise
function dadBranch() {
var dadStrategy = askDadForMoney()
.then(buyMangoes, sitAndCry);
return dadStrategy;
}
getMoneyFromAccount()
.then(buyMangoes, dadBranch)
.then(eatMangoes)
@tmpaul06
tmpaul06 / mango_promise_error.js
Last active July 6, 2017 07:11
Promise error handling
getMoneyFromAccount()
// buyMangoes here will throw an Error
.then(buyMangoes, dadBranch)
// Next handler in chain
.then(eatMangoes, handleThief)
The `handleThief` function is called when buyMangoes throws an exception