request-compose icon indicating copy to clipboard operation
request-compose copied to clipboard

Wow, What an amazing package!

Open mike442144 opened this issue 6 years ago • 4 comments
trafficstars

The idea is almost same with mine. Since that the request.js package is difficult to maintain and extend. Wow, I should consider the module in my next refactor of crawler. Years ago I published my distributed crawler framework based on middleware called floodesh, and these years I've been thinking about how to construct the single-node version of crawler, the most import idea is composable like my design in floodesh, which is also inspired by koa compose. Glad to see this, I'm watching the great work.

mike442144 avatar Jan 29 '19 12:01 mike442144

Thanks @mike442144, I really appreciate it!

The middleware approach is very powerful indeed. The idea here is that you have a simple compose function that calls each one of it's arguments and pass the result to the next one. The best part is that you can continue applying the same compose function over and over again on every subsequent layer on top of the client.

This approach also implies that there is no state, beyond the current request. All functions in the chain return a Promise, meaning that all errors are catched too.

In the Functional Paradigm, and in the programming languages that supports this natively, it's also known as pipe operator |> There is really no difference between the compose function that this module exposes, and the pipe operator, except syntactical.

I wrote a short article about the module here: https://dev.to/simov/composable-http-client-for-nodejs-83f

simov avatar Jan 30 '19 14:01 simov

Yep, the Functional way is cool! I noticed your signature which seems like lisp? I'm now practicing FP use ramda and ramda-fantasy in my projects after reading some Haskell materials. It really empowers the code expressiveness. Thanks to your high quality article.

mike442144 avatar Jan 31 '19 08:01 mike442144

Yeah, I really do see similarities between Scheme (that is a Lisp) and JavaScript. Plus, the two main ingredients in JavaScript comes from:

  • Scheme (the FP part, Lisp)
  • Self (the prototypical OOP, Smalltalk)

I have another article where I experimented with the limits of what is possible with the JS syntax: https://dev.to/simov/anonymous-recursion-in-javascript

If you put the two examples side by side you'll notice that only the syntax differs. The underlying features such as Lexical Scope and First Class Functions work identically.

simov avatar Jan 31 '19 09:01 simov

I'm not familiar with Scheme, but I tried the example in your article, I'm really surprised! I had no idea how to do anonymous recursion before, even an assignment like this:

const fn = (n) => n==0 ? 0 : fn(n-1)+fn(n-2);

I got error fn is not defined. Your article inspired me! I'll try it out. Thank you so much!!!

mike442144 avatar Jan 31 '19 12:01 mike442144