Ky’s (Somewhat Poignant) Guide to Asynchronicity and Cryptocurrencies — Part 4
(Also inconsistently referred to as Ky’s Guide to Starting Over 1.8.4 or Ky’s Guide to Cryptocurrency Conclusions in future annotations and subsequent editions)
One Click
Every was as it should have been: the winter wind had accepted I was not partaking in its seasonal games and had shuffled off with a few strips of vinyl siding in hand, the investment banking spiders had retreated to their webs banking establishment, Gotsham Regional Credit Union, and I had finally put together my app to check on prices of different cryptocurrencies, CryptoChecker.
I had been able to take a more comprehensive understanding of different components in JS and combine them to create an app that would let me get prices for two different currencies of interest, Bitcoin and Ethereum, plus a pricing metric on one of the rarest marbles, the Razorback Sulfite.
Knowing how async code worked was great and understanding the mechanism for making and handling requests with JS was stupendous and being able to use promises to create cleaner async code was the bee’s knees. But what did all that matter if I didn’t put it all to use, right?
Good, But Still “Pyramidal” …
I uploaded my app and clicked away; everything was working! Huzzah!
After some brief testing, I was satisfied with its functionality. I could click on one button and get three prices updated right there on the screen.
But there was something off, some piece of this that was holding a feather duster up and wandering through my synaptic hallways, tsk-tsking about “keeping up appearances.”
After some brief evaluation, I realized that there were several things I could improve through some refactoring.
I had broken pieces of the program up into separate classes to separate concerns but could do a better job of this by building out a class to handle the calls I needed to make to the API.
By using a class like the above, I would leverage promises so that any method that needed the data could be caught in a `Promise.prototype.then` invocation. The `fetch` method is part of an extremely useful Web API; it can do many things, but at the simplest level, it can make an XHR request to a provided URL and return a promise with the results. I could use that API to keep this class very streamlined and make use of the power of promises.
I could also refactor the `UI` class to have a simpler `updatePrices` method that would take care of all updates at once. Since I wouldn’t be checking for any of the prices in isolation in this app, I might as well update them all at once, since I would be getting all their data anyways.
The callback that would handle the update functionality when the `Update` button was clicked was lengthy; it indeed looked a bit like a pyramid… It should have been streamlined leveraging promises, and by simplifying the URL being used for the request to include both currencies. That would but a lot of extra lines out and make it much easier to read.
A Bit of Elbow Grease and Beef Stew
Working under the light of my many monitors, it took less time that I expected to get my updates made. I had only gotten through half a tin of stew and it was already done. Was it rudimentary? Absolutely. Was it perfect? Not by a long shot. But did it work, and do what I needed it to? Yes. Was it easier than having to negotiate a handful of sites to get the prices I wanted? Yes!
There were improvements to be made and new features to be added as the needs arose. What about all the other API interfaces that are provided? Certainly, I could find a use for some of those? I could also include a wider range of coins, or even provide some kind of selection menu to choose from a list of coins to get the prices of.
There were many improvements that could be added and perhaps some enterprising reader with an interest in the subject and a bit of fire in their belly will take this opportunity to work on the project as a means of practicing their craft. But for now, for my purposes it’s enough.
To be continued, or not… We’ll see.
Until next time, or not… We’ll see.
-Ky
Postscript
The story started here. Find out why I started building this app. Also, why you shouldn’t trust investment banking spiders. ## link for first story ##
The story continued here. Find out how requests can be made with JS and why pyramids are better left to the Egyptians. ## link for first story ##
The story’s penultimate chapter; promises and pickled herring.