Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Using Node.js for simple step-by-step tasks without setTimeout?

I've recently started to mess around with Node.js to build web scrapers and other small scripts. Recently I built a simple js script that does the following:

1) Goes to a website and downloads a gif

2) Converts that gif from single loop to infinite loop

3) Tweets that gif on Twitter

Without a setTimeout, the code would try to convert the gif before the download is complete, etc. etc., completely breaking.

To get around this, I used setTimeout to delay each action for x seconds.

However warning bells were going off in my head that the setTimeout is probably not the best way to create this step by step process.

So what I want to know is, can you do step-by-step processes with Node.js easily, or is it "the wrong tool for the job" when it comes to this automation?

OR, would callback functions be the correct way to go with this type of script? I've read that callbacks should be avoided because the code gets really messy.

OR, would it be better to break each step into its own js file and run them after each other in node?

OR, should I be looking into a js "task runner" like Gulp or Grunt? (I don't really know how these work atm. The treehouse course seems to be about getting apps ready for production, not necessarily what I need.).

Thanks.

2 Answers

I'm not certain, but you should be able to do all that in Node with the help from additional modules/libraries. Changing the loop settings for a gif sounds tricky but I bet it can be done.

As for the callbacks, yes they can get messy, but if you declare your callback functions beforehand and in separate files, instead of anonymously, you can keep your code pretty clean. Visit Callback Hell for some details on cleaning up callback code.

Thank you for the response!

The gif loop was not an issue. I was able to use graphicsmagick and its node module to edit the image.

The program I was talking about works for what I need it to do, but going forward creating new apps, I was wondering what preferred way in node to accomplish step-by-step tasks.

No problem! There really is no "preferred way" of doing this, it's just a matter of finding your own preferred way.

You have some other options to avoid "Callback Hell" as well. Look into promises (bluebird is a good module for those), async.js, and ES6 generators. You might want to consider making it using callbacks first for the learning experience, and then you can pretty easily switch from regular callbacks to something like async.js.