Fork me on GitHub

Creating new functions

Creating simple functions is easy.


To create a callback-taking function, add a callback argument to your function. Then you can pass the callback to another callback-taking function.

function readMyFile(callback) {
    fs.readFile('myfile.txt', callback); 


To create a promise-based function, simply return the promise as a result.

function readMyFile() {
    return fs.readFileAsync('myfile.txt');

But what if you want to do additional processing? Then you'll also need to create your own callback, or use the power of .then().