Last active
January 21, 2018 08:51
-
-
Save ollyjshaw/82adb1b14ea057f19a77bc3d804dcaaa to your computer and use it in GitHub Desktop.
A playground explaining promise things in ES6
This file contains hidden or 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
/* | |
Promises are fun by Olly Shaw | |
https://twitter.com/olly_shaw | |
https://github.com/ollyjshaw | |
To run this download the file and do something like | |
node promises.js | |
logging will be out of order (Promises are asychronous). But hopefully you can find your way. | |
*/ | |
let greetingPromise = Promise.resolve("hi") | |
//it's a promise | |
console.log(greetingPromise) //Promise { 'hi' } | |
//will print the greeting out | |
greetingPromise.then((value) => console.log(value)) //hi | |
//you can throw an error but it must be caught in the chain | |
greetingPromise.then((value) => { | |
console.log(value) | |
throw Error("foobar mk1") | |
}).catch(err => console.warn(`caught an error: ${err}`)) //caught an error: Error: foobar mk1 | |
//not being in a chain will bubble the error up and give a | |
// UnhandledPromiseRejectionWarning: | |
// DeprecationWarning: Unhandled promise rejections are deprecated. | |
// In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. | |
greetingPromise.then((value) => { | |
console.log(value) | |
throw Error("foobar") | |
}) //UnhandledPromiseRejectionWarning | |
//Try catch will not save you here, this will still give a UnhandledPromiseRejectionWarning | |
try { | |
greetingPromise.then(() => { | |
throw Error("foobar") | |
}) | |
} catch (error){ | |
console.log("I caught the error", error) | |
} //UnhandledPromiseRejectionWarning | |
//Start off some number chains | |
let numberPromise = Promise.resolve(42) | |
//chaining requires that a function is given to the then method | |
//even if no value is returned or used. | |
greetingPromise.then((greetValue) => { | |
console.log(`greetValue is: ${greetValue}`) //hi | |
}).then(()=>numberPromise).then(numberValue => { | |
console.log(`numberValue is ${numberValue}`) //42 | |
}) | |
//if you just put the reference to a promise into the then, what happens is | |
//the promise reference is wrapped in a thenable. The thenable will just produce an `undefined` | |
greetingPromise.then((greetValue) => { | |
console.log(`greetValue is: ${greetValue}`) //hi | |
}).then(numberPromise).then(thenableThing => { | |
console.log(`thenableThing is ${thenableThing}`) //undefined | |
}) | |
//if you return a value from a then, it is passed down the promise chain | |
greetingPromise.then(() => { | |
return 3 | |
}).then(fromThenBlockNumberValue => { | |
console.log(`fromThenBlockNumberValue is ${fromThenBlockNumberValue}`) //3 | |
}) | |
// If you want to pass a parameter to a promise, simply wrap a it in a function | |
function doubler(foo) { | |
return Promise.resolve(foo*2) | |
} | |
//A function returning a promise can then really unlock some cool stuff. | |
numberPromise.then((value)=> doubler(value)).then(doubledValue => { | |
console.log(`doubledValue is ${doubledValue}`) //84 | |
}) | |
//Beware the use of blocks in the then | |
numberPromise.then((value)=> { | |
doubler(value) | |
}).then(nothingWasReturnedFromBlock => { | |
console.log(`nothingWasReturnedFromBlock is ${nothingWasReturnedFromBlock}`) //undefined | |
}) | |
//if you need a block you must return | |
numberPromise.then((value)=> { | |
return doubler(value) | |
}).then(returnedFromBlock => { | |
console.log(`returnedFromBlock is ${returnedFromBlock}`) //84 | |
}) | |
//If you have an error in the chain, further promise chaining is skipped and | |
//the catch clause is hit. | |
numberPromise.then(() => { | |
throw Error("foobar mk2") | |
}).then(()=>greetingPromise).then(() => { | |
console.log("You won't see this") | |
}).then(() => { | |
console.log("Or this") | |
}).then(() => { | |
throw Error("This Error won't be seen either") | |
}).catch(err => console.warn(`dealing with the error: ${err}`)) //dealing with the error: Error: foobar mk2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment