Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise patterns #4

Open
anandkumarpatel opened this issue Jul 27, 2016 · 3 comments
Open

Promise patterns #4

anandkumarpatel opened this issue Jul 27, 2016 · 3 comments

Comments

@anandkumarpatel
Copy link
Contributor

anandkumarpatel commented Jul 27, 2016

Let us discuss our new coding patterns now that we moved everything to promises:

Method 1 Linear program w/ comments

Promise.try(() {
  // JSDoc
  ... code ...
})
.then(doSomething1 () {
  // JSDoc
  ... code ...
})
.then(doSomething2 () {
  // JSDoc
  ... code ...
})
.then(doSomething3 () {
  // JSDoc
  ... code ...
})

Pro: looks linear
Cons: unit testing is harder, requires more setup

Method 2 Functional

Promise.try(() {
 return fn1()
})
.then(() {
  return fn2()
})
.then(() {
 return fn3()
})
.then(() {
 return fn4()
})
// JSDoc
fn1 = {
  ... code ...
}
// JSDoc
fn2 = {
  ... code ...
}
// JSDoc
fn3 = {
  ... code ...
}
// JSDoc
fn4 = {
  ... code ...
}

Pro: functions are broken up into smaller components. Each function can have proper unit tests and docs
Con: have to look at different areas on the page to understand full function.

Any other methods people prefer & which do you like the most?

@podviaznikov
Copy link
Member

podviaznikov commented Jul 27, 2016

Another solution is to add names to anonymous functions.

Promise.try(() {
  return fn1()
})
.then(doSomething1 () {
  return fn2()
})
.then(doSomething2 () {
  return fn3()
})
.then(doSomething3 () {
  return fn4()
})

That way you still can collapse all functions and see "the big picture" instead of seeing this:
instance_container_redeploy_js_ __users_podviaznikov_runnable_api
and reading function implementation in order to understand what function does and what it returns

@thejsj
Copy link
Member

thejsj commented Jul 27, 2016

Code folding is fot n00bs, but I like @podviaznikov's suggestion. I think name methods work well. I do like keep the linear structure of it.

Also, I think we could add some JSDoc between these methods:

Promise.try(() {
  return fn1()
})
/**
 * @param {String} name
 * @returns {Promise}
 * @resolves {String}
 */
.then(doSomething1 (name) {
  return fn2()
})
/**
 * @param {String} name
 * @returns {Promise}
 * @resolves {String}
 */
.then(doSomething2 (name) {
  return fn3()
})\/**
 * @param {String} name
 * @returns {Promise}
 * @resolves {String}
 */
.then(doSomething3 (name) {
  return fn4()
})

@podviaznikov
Copy link
Member

haha, code folding is workaround in order to achieve same feature as Method 2 provides. I'd rather have Method 2, but code folding with Method 3 would work for me too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants