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

Use Promises #55

Closed
raineorshine opened this issue Feb 16, 2015 · 9 comments
Closed

Use Promises #55

raineorshine opened this issue Feb 16, 2015 · 9 comments
Labels

Comments

@raineorshine
Copy link
Owner

Callbacks are so 2010.

@zensh
Copy link

zensh commented Mar 20, 2015

I suggest using https://github.com/thunks/thunks.
I can refactor it with thunks if you want.

@raineorshine
Copy link
Owner Author

Looks cool... I need to evaluate that more deeply to understand it. I know that promises are a higher level of abstraction than control flow like async.js, which is why I wanted to go that direction. What is the relationship between thunks and promises?

@zensh
Copy link

zensh commented Mar 20, 2015

  1. Native Promise isn't a control flow tool, thunks provide .all | . seq | . race and some other method for control flow.
  2. thunks is very small (288 SLOC), compatible for node.js 0.10.x
  3. thunks is function style, promise is object style
  4. thunks is also compatible for promise | generator | node callback | stream https://github.com/thunks/thunk-stream

@raineorshine
Copy link
Owner Author

Okay, I'm beginning to wrap my head around this. Any asynchronous (or synchronous) function can be wrapped in a thunk to represent a deferred value with a well-defined interface for accessing the result. It's basically just done as a function instead of a Promise object.

I'm still a bit unclear on why you would use Thunks over Promises or vice versa. Googling "thunks vs promises" yielded surprisingly little.

It seems like thunks are a bit lower level than promises. Consequently they are more lightweight, but are less robust in some situations.

"Thunk will not catch exception within execute function (from thunk( executeFn ))"
"thunkFunction(cb) will invoke cb as soon as data is ready, which means if data is already there, cb will be invoke immediately / synchronously.
https://github.com/gyson/gocsp-thunk

"You may notice that this will lead the execution of cb be indeterministic (aka. zalgo). Then following code will help you to check if it's sync or async... you can always convert thunk function to promise if you want to ensure zalgo-free."
https://github.com/gyson/gocsp-thunk

"If you don’t like or understand this, just use promises. Promises are almost certainly a better choice for application code. Thunks are only good as a low-level primitive used internally by Koa because they don’t involve any particular promises library."
http://blog.stevensanderson.com/2013/12/21/experiments-with-koa-and-javascript-generators/

"Comparing thunks to promises generally: they’re not directly interchangable as they’re not equivalent in behavior. Promises are vastly more capable and trustable than bare thunks."
https://www.safaribooksonline.com/library/view/you-dont-know/9781491905197/ch04.html

With efficient Promise libraries like bluebird that have all, seq, etc as well as robust error handling, I feel like there's no reason not to use Promises.

I appreciate your offer to refactor using thunks, and I'm wondering what your thoughts are on the Promise advantages mentioned above. Thanks!

-Raine

@zensh
Copy link

zensh commented Mar 23, 2015

there is something wrong in https://github.com/gyson/gocsp-thunk:

  1. thunks is lazy evalution, that is different in essence from promise;
  2. thunks is chaining implementation;
  3. Do Not Release Zalgo is a guideline for Designing APIs(asynchronous or synchronous, not maybe). It is not belong to thunk

@raineorshine
Copy link
Owner Author

Okay, perhaps gocsp-thunk was not the best library to reference. Returning to the broader point however, I'm curious what you think about some of the other comments I saw about Promises being more reliable:

"Comparing thunks to promises generally: they’re not directly interchangable as they’re not equivalent in behavior. Promises are vastly more capable and trustable than bare thunks."
https://www.safaribooksonline.com/library/view/you-dont-know/9781491905197/ch04.html

I am just wanting to make sure I properly understand the differences/tradeoffs.

@zensh
Copy link

zensh commented Mar 24, 2015

You can try Promise first~

@raineorshine
Copy link
Owner Author

Okay. Thanks for your great input! Good to be thinking about this stuff
more.

On Mon, Mar 23, 2015 at 8:52 PM, Yan Qing notifications@github.com wrote:

You can try Promise first~


Reply to this email directly or view it on GitHub
#55 (comment)
.

@raineorshine
Copy link
Owner Author

Promises are in! ^_^

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

No branches or pull requests

2 participants