This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
promise | |
.then(buyMangoes, dadStrategy); | |
// No error. No warning. Error is simply gone. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dadBranch() { | |
var dadStrategy = askDadForMoney() | |
.then(buyMangoes, sitAndCry); | |
return dadStrategy; | |
} | |
getMoneyFromAccount() | |
.then(buyMangoes, dadBranch) | |
.then(eatMangoes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The first call returns promise1 | |
remoteDataCall().then(function (result) {}); | |
// Second call returns another promise2 !== promise1 | |
remoteDataCall().then(function (result) {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var promise = remoteDataCall(); | |
promise.then(function (result) {}); | |
promise.then(function (result) {}); | |
// Same result as above without executing `remoteDataCall` again. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
OlderNewer