My First Steem.js Adventure, Part 4: Fixing My API Timeout Bug & Finishing Up!

hooray.b10c443a5c564fa3b6183b4536b9c8e2.png

Emoji Source

Posts in this series: Part 1 / Part 2 / Part 3 / Part 4

I finished my first STEEM project!

In my previous post, I said that it might be the final in my series about creating my first Steem.js tool, but I just got some help from my mentor and I feel like it would be valuable to share what fixed the problem.

The bug was that if I searched with too many users, the API calls would get hung up. It looks like the problem might have been a security feature on the STEEM API that wanted to make sure I wasn’t doing anything malicious. To fix this I just needed to put a small gap of time between some of the calls.

The Fix


The solution for this problem only took a few lines of code. First was a function to create a promise that waits a specified amount of time:

function sleep(ms) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve();
        }, ms);
    })
}

 
Then I just had to shuffle this function into my big array of API calls. Before my fix, I used map to wrap promises around my STEEM API calls like this:

let namePromiseArray = usersToSearch.map(name => createHistoryPromise(name, transactionsCount, startDate, endDate));

 
My fix was to iterate over usersToSearch and execute my sleep function every fifth iteration:

let namePromiseArray = [];

for (let [index, name] of usersToSearch.entries()) {
    if (index % 5 === 0) await sleep(1000);
    namePromiseArray.push(createHistoryPromise(name, transactionsCount, startDate, endDate))
}

 
You might have noticed that I used the await keyword again to wait for my sleep function to execute. To make this possible, I just made the callback function surrounding the for loop an async function.

Monitoring the API Requests With Chrome Dev Tools


The way I’ve been monitoring all of the API calls to make sure they go through, is with the Network section in Chrome dev tools. You can open up the dev tools by right clicking on the web page and selecting inspect. If you click Network at the top, it will show you all of the network requests that are happening:

Screen Shot 2018-01-26 at 4.06.28 PM.3842ee8e5dae4b918033840d8412298f.png

If I look at this pane after clicking search in my app, I see the requests as they’re executed:

network_requests.d74c77d5faa441a5bf9948c3b11b5484.gif

This is how I found that some of the requests weren’t going through before. A group of them went through pretty quickly, and then the others got hung up until I received an error.

Cleaning Up My Code & Publishing


The first order of business to get things finalized was to add a pacman loading spinner:

Screen Shot 2018-01-26 at 11.26.34 AM.8f85dc42d83d44da86d89ac7bc55f838.png

Then I wanted to add a message in red at the top of the list if the app needed more transactions to process the data properly. At first I just had the message sent back in the 'total' field on the table:

Screen Shot 2018-01-26 at 11.29.07 AM.21e58d2a5a2645a09026c7590cd1b73d copy.6bbeeb60c7654034a4587e5948afb809.png

So I created a small function to look through the table and see if that message was returned.

function checkResultsForError(resultsArray) {
    let found = resultsArray.some(function (el) {
        return el.total === 'need more transactions';
    });
    return found;
}

 
Then I showed the message with this:

let error = checkResultsForError(resultsArray);
if (error) $('.error').text('The app needs more transactions for this search. Increase the value above and search again.');

 
I then added a new div to my html along with a one line css file. Here’s the result:

Screen Shot 2018-01-26 at 11.40.52 AM.adf694a15d1a40169efce624bd1c7d24.png

And that’s it! Here’s the final product in action:

steem-power-rankings.e575849b6cf84b788f6a3ce9e499da7a.gif

Removing Node & Publishing


After I fixed everything up, I moved the jQuery and Steem.js files into my main folder and then updated the file paths to get rid of Node. Then I enabled Github Pages on my repo and it is now live here. If you try it out and find any bugs, please let me know here or on Github!

Making this app has been a good example of what I’m enjoying so much about coding. I’m learning this skill to help me get a job, but I just used it to help solve a problem. That’s so cool! I can’t wait to see other opportunities to use what I’ve learned as I get more proficient.

Thanks so much for following along with my coding adventure! It feels so good to have this first app out there in the wild and being used by people to save some time. 🙂

Posts in this series: Part 1 / Part 2 / Part 3 / Part 4


follow_jeffbernst.gif

H2
H3
H4
3 columns
2 columns
1 column
21 Comments