I have the following function within a CoffeeScript class:
use: (db, fn) ->
fs.exists db, (exists) =>
if exists?
@db = db
@_fetch()
else
fs.writeFile db, {}, (err) =>
if err
err
else
@db = db
@_fetch()
What bothers me is that I had to write @db = db and @_fetch() twice. The reason I had to do this was that the else clause calls on fs.writeFile which is asynchronous. Therefore, I had to put those two lines inside writeFile's callback to make sure they didn't fire too soon.
Is there a way, however, to clean this up and write those lines ones? It's only two lines at the moment, but what if the callback exists of 10 lines? Would get messy, no?
Any other feedback on this small piece of code is welcome too. Not that sure about the error handling (just returning err at the moment..).
CoffeeScript has functions and => allows you to bind a function to an instance so why not just chop your code up into bound functions?
use: (db, fn) ->
fetch = =>
@db = db
@_fetch()
fs.exists db, (exists) =>
if exists?
fetch()
else
fs.writeFile db, {}, (err) =>
if err
err
else
fetch()
Then add whatever else (such as calling fn) needs to be done to fetch.
Also, I don't think your:
if err
err
error handling does anything. I'm not a node.js expert but, AFAIK, writeFile doesn't care what the callback returns.
You can introduce a helper function:
use: (db, fn) ->
helper = () =>
@db = db
@_fetch()
…
fn(null, result)
fs.exists db, (exists) =>
if exists
helper()
else
fs.writeFile db, {}, (err) ->
if err
fn(err)
else
helper()
return
return
Since the only time you don't want to load the DB and fetch is if the db does not exist and the subsequent loading produces an error, you could try something like this...
use: (db, fn) ->
fs.exists db, (exists) =>
if not exists?
fs.writeFile db, {}, (err) =>
if err
return err
@db = db
@_fetch()