Created
August 11, 2021 13:14
-
-
Save kevinwkc/f13f775757794e6af137fa2371d91472 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/nawuyer
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
Promise.prototype.map = function (mapping) { | |
var initialPromise = this; | |
return new Promise(function (resolve, reject) { | |
initialPromise.then(result => resolve(mapping(result))).catch(reject); | |
//.catch() path of failure and ignores the f mapping | |
}); | |
}; | |
var id = x=>x; | |
var value = 5 | |
// Law 1 - the identity is preserved | |
new Promise((resolve) => resolve(value)).map(id) | |
.then(x=>console.log(x===id(value))) // true | |
// Law 2 composition of functions is preserved | |
var f =x=>2*x; | |
var g =x=>3+x; | |
new Promise((resolve) => resolve(value)).map(x=>f(g(x))).then(console.log); | |
new Promise((resolve) => resolve(value)).map(f).map(g).then(console.log); | |
new Promise((resolve) => resolve(value)).map(g).map(f).then(console.log); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> Promise.prototype.map = function (mapping) { | |
var initialPromise = this; | |
return new Promise(function (resolve, reject) { | |
initialPromise.then(result => resolve(mapping(result))).catch(reject); | |
//.catch() path of failure and ignores the f mapping | |
}); | |
}; | |
var id = x=>x; | |
var value = 5 | |
// Law 1 - the identity is preserved | |
new Promise((resolve) => resolve(value)).map(id) | |
.then(x=>console.log(x===id(value))) // true | |
// Law 2 composition of functions is preserved | |
var f =x=>2*x; | |
var g =x=>3+x; | |
new Promise((resolve) => resolve(value)).map(x=>f(g(x))).then(console.log); | |
new Promise((resolve) => resolve(value)).map(f).map(g).then(console.log); | |
new Promise((resolve) => resolve(value)).map(g).map(f).then(console.log);</script></body> | |
</html> |
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.prototype.map = function (mapping) { | |
var initialPromise = this; | |
return new Promise(function (resolve, reject) { | |
initialPromise.then(result => resolve(mapping(result))).catch(reject); | |
//.catch() path of failure and ignores the f mapping | |
}); | |
}; | |
var id = x=>x; | |
var value = 5 | |
// Law 1 - the identity is preserved | |
new Promise((resolve) => resolve(value)).map(id) | |
.then(x=>console.log(x===id(value))) // true | |
// Law 2 composition of functions is preserved | |
var f =x=>2*x; | |
var g =x=>3+x; | |
new Promise((resolve) => resolve(value)).map(x=>f(g(x))).then(console.log); | |
new Promise((resolve) => resolve(value)).map(f).map(g).then(console.log); | |
new Promise((resolve) => resolve(value)).map(g).map(f).then(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment