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
const { promisify } from "util"; | |
const evenSuccess = (n, onSuccess, onError) => { | |
if (n % 2) { | |
onError(n); | |
} else { | |
onSuccess(n); | |
} | |
}; |
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
const CUSTOM = Symbol("promisify.custom"); | |
const promisify = fn => { | |
if (fn[CUSTOM]) { | |
return fn[CUSTOM]; | |
} | |
return (...args) => new Promise((resolve, reject) => { | |
fn(...args, (error, value) => { | |
if (error) { |
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
const { callbackify } = require("util"); | |
const evenSuccess = n => n % 2 ? Promise.reject("odd") : Promise.resolve("even"); | |
callbackify(evenSuccess)(1, console.log); // "odd" | |
callbackify(evenSuccess)(2, console.log); // undefined "even" |
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
const curry = fn => (...args) => | |
args.length >= fn.length | |
? fn(...args) | |
: (...innerArgs) => fn(...args, ...innerArgs); |
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
const curry = fn => { | |
const curryN = (n, fn) => (...args) => | |
args.length >= n | |
? fn(...args) | |
: curryN(n - args.length, (...innerArgs) => fn(...args, ...innerArgs)); | |
return curryN(fn.length, fn); | |
}; |
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
const expect = x => ({ | |
toBe: y => { | |
if (x !== y) { | |
throw new Error(`expected ${x} to be ${y}`); | |
} | |
} | |
}); | |
const sum4 = curry((a, b, c, d) => a + b + c + d); |
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
const curry = fn => (...args) => | |
args.length >= fn.length | |
? fn(...args) | |
: curry((...innerArgs) => fn(...args, ...innerArgs)); |
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
expect(sum4(1)(2)(3,4)).toBe(10); | |
expect(sum4(1)(2)(3)(4)).toBe(10); | |
expect(sum4(1,2)(3)(4)).toBe(10); |
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 { curry, __ as _ } from "ramda"; | |
const sum4 = curry((a, b, c, d) => a + b + c + d); | |
// Currying | |
sum4(1, 2, 3, 4); | |
sum4()(1, 2, 3, 4); | |
sum4(1)(2, 3, 4); | |
sum4(1)(2)(3, 4); | |
sum4(1)(2)(3)(4); |
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
// Same as promise.js but with debug messages. | |
// Just run the code with node promise-debug.js. | |
// By setting DEBUG to true each promise will output in console some debug info | |
// There are 13 samples. In terms of debug output is better to focus in one of them at a time. | |
// You can do that by setting the FOCUS variable (i.e. FOCUS=11) | |
const DEBUG = false; | |
const FOCUS = false; | |
let promiseId = 1; |