Created
November 28, 2017 16:41
-
-
Save anatomic/5ef4cea94aafca28a140aaef316ac756 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/supehey
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"> | |
const p = Promise.resolve("This has succeeded") | |
const makeLogger = method => l => (console[method](l), l); | |
const log = makeLogger("log"); | |
const error = makeLogger("error"); | |
p.then(log) // promises are a bit funky, they behave differently depending on the return type | |
.then(m => Promise.resolve(m)) | |
.then(log) // returning a promise acts like a Monad chain, you don't get a promise of a promise, it's unwrapped | |
.then(m => m) | |
.then(log) // but if you return a value it's also returned in a promise. Confusing? | |
.then(m => Promise.reject("Don't throw errors, instead return a rejected promise")) | |
.then(log) // but if you return a rejected promise, you end up heading down the "rejection" path, and `then` won't do anything | |
.catch(error) // throwing and returning a rejected promise can both result in swallowed errors, it's just one is pure and the other, isn't | |
.then(log) // even more confusingly, returning a value or a promise in a rejection chain will slip to the resolution chain. | |
.then(m => Promise.reject("Confused yet?")) | |
.then(log, error) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const p = Promise.resolve("This has succeeded") | |
const makeLogger = method => l => (console[method](l), l); | |
const log = makeLogger("log"); | |
const error = makeLogger("error"); | |
p.then(log) // promises are a bit funky, they behave differently depending on the return type | |
.then(m => Promise.resolve(m)) | |
.then(log) // returning a promise acts like a Monad chain, you don't get a promise of a promise, it's unwrapped | |
.then(m => m) | |
.then(log) // but if you return a value it's also returned in a promise. Confusing? | |
.then(m => Promise.reject("Don't throw errors, instead return a rejected promise")) | |
.then(log) // but if you return a rejected promise, you end up heading down the "rejection" path, and `then` won't do anything | |
.catch(error) // throwing and returning a rejected promise can both result in swallowed errors, it's just one is pure and the other, isn't | |
.then(log) // even more confusingly, returning a value or a promise in a rejection chain will slip to the resolution chain. | |
.then(m => Promise.reject("Confused yet?")) | |
.then(log, error) | |
</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
const p = Promise.resolve("This has succeeded") | |
const makeLogger = method => l => (console[method](l), l); | |
const log = makeLogger("log"); | |
const error = makeLogger("error"); | |
p.then(log) // promises are a bit funky, they behave differently depending on the return type | |
.then(m => Promise.resolve(m)) | |
.then(log) // returning a promise acts like a Monad chain, you don't get a promise of a promise, it's unwrapped | |
.then(m => m) | |
.then(log) // but if you return a value it's also returned in a promise. Confusing? | |
.then(m => Promise.reject("Don't throw errors, instead return a rejected promise")) | |
.then(log) // but if you return a rejected promise, you end up heading down the "rejection" path, and `then` won't do anything | |
.catch(error) // throwing and returning a rejected promise can both result in swallowed errors, it's just one is pure and the other, isn't | |
.then(log) // even more confusingly, returning a value or a promise in a rejection chain will slip to the resolution chain. | |
.then(m => Promise.reject("Confused yet?")) | |
.then(log, error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment