Skip to content

Instantly share code, notes, and snippets.

@markzyu
Created February 18, 2016 06:20
Show Gist options
  • Save markzyu/6dd1b5d12d680f981432 to your computer and use it in GitHub Desktop.
Save markzyu/6dd1b5d12d680f981432 to your computer and use it in GitHub Desktop.
Findings about Promise
// just run the following code in one line in node console
// The creator of Promise is responsible to report whether the 1st action is successful or not
// cases:
// neither fulfill nor rej is called -> the "then()"s will never be called and this promise is pending
// if fulfill is called -> the value put into fulfill() will be passed to the "then()" chains
// !! if one then() broke due to grammar or Runtime errors, following then()s will not execute, but
// any catch following that will execute
> new Promise((fullfill, rej) => fullfill(100)).then(x => asdf).catch(e => console.log(e))
Promise { <pending> }
> [ReferenceError: asdf is not defined]
// if rej() is called -> the value put into rej() will be passed to the first "catch()"
// !! if after catch() is a next(), then it WILL be executed!
> new Promise((fullfill, rej) => rej(100)).catch(x => console.log(x)).then(y => console.log(123))
Promise { <pending> }
> 100
123
// in short: for the first action it can only fulfill by calling fulfill but not by returning.
// But for the rest of the chain: throwing <=> reject. returning <=> fulfill, and it will fulfill even if inside a catch().
/// ---- NO nested promises: we just return the created promise, we do not need to use nested "then({then({then("
new Promise((fulfill, rej) => fulfill(10))
.then(() => new Promise((ful, rej) => ful(100))
.then(x => {console.log("hehe");return x;})).then(x => console.log(x))
.then(anotherFunc)
rather than:
new Promise((fulfill, rej) => fulfill(10))
.then(() => {
new Promise((ful, rej) => ful(100))
.then(x => {console.log("hehe");return x;})).then(x => console.log(x))
}.then(anotherFunc)
// Thus Promise is truly different from ActionChain...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment