Pages

Showing posts with label nodejs. Show all posts
Showing posts with label nodejs. Show all posts

[NodeJS] What is a thunk?

What is a thunk?

A thunk, as Michael Fogus defines in his book "Functional Javascript", is "a function that wraps some behavior for later execuation". Specifically, in Node.js, a thunk of a function fn is a function that partially applies all the arguments of fn except the callback. As a result, the returned function accepts only one argument, which is a callback. For example, let's take a look at the readFile function. Its syntax is as following.


var readFile = require('fs').readFile;

readFile('path/to/file', 'utf-8', function(err, data) {
  if (err) throw err;
  console.log(data);
});

[NodeJS] How to behave synchronously in an asynchronous world

Dispite the power of asynchronous programming in Javascript, there's time when you want to execute code synchronously. An example is when you have an array of tasks that must be completed sequentially. In this post, we are going to look at different ways to implement this in NodeJS.

Settings

Image you want to simulate the activities in a restaurants. The tasks are: server takes order, chef cooks, server serves food, customers enjoy the food. Since the level of details of simulation can be expanded in the future, we'll maintain an array of all the tasks and pass it to a task runner to handle the tasks.


function order() { console.log('Server gets order.'); }
function cook() {
  fs.readFile('cookbook.txt', function() {
    console.log('Chef cooks food.');
  };
}
function serve() { console.log('Server serves food.'); }
function dine() { console.log('Customers enjoy food.'); }

function run(tasks) {
  // run tasks
}

var tasks = [order, cook, serve, dine];
run(tasks);

As you can see, among these tasks, cook is the longest one because chef must find the recipe in a cookbok before starting to cook. Obviously, it's not a very good restaurant as you can tell but you get the idea.

[NodeJS] Command line tool

Basics

With NodeJS, it's fairly simple to create a command-line tool. First, you need to add bin field into package.json file.


# File: package.json
"bin": {
    "command1": "path/to/command1/implementation",
    "command2": "path/to/command2/implementation",
    "command3": "path/to/command3/implementation"
}

You can have as many commands as you want. I like to store all the executable files in bin/ directory. Let say I want to create a command named node-greet to simply print out a greeting message. Here's the bin field of my package.json

[NodeJS] Pushing posts to Blogger from commandline

I was trying to write some small cli to push my posts to Blogger from the commandline. There are some tools out there, including googlecl and blogger-cli but no nodejs tool as far as I know. So, I registered to use Blogger API. Unfortunately, you'll have to wait 5 days just to get approval and instruction from Google, and that process is handled by only one man, Brett. So, while waiting for the API, here's my work around.