 
 # pass `schedules` in
 .then () ->
-	console.log schedules
 	return schedules
 
 # process data
 .then computeDaysInSchedules
+
+# write data
+.then writeNdjson 'schedules.ndjson'
 .catch (err) ->
 	console.error err.stack
 
-# write data
-.then writeNdjson 'schedules.ndjson'
+.then () ->
+	console.log 'Done.'

		if not schedule or not schedule.start then return

		date = parseDate(data.date).valueOf()
		# if not schedule.available or not schedule.notAvailable
		# 	console.log schedule
		if not (data.exception_type - 1)
			schedule.available.push date
		else schedule.notAvailable.push date


writeNdjson = (file) ->
	return (schedules) ->
		task = Q.defer()

		stringify = ndjson.stringify()
		stringify.on 'error', (err) ->
			console.error err.stack   # todo: remove?
			task.reject err

		writeStream = fs.createWriteStream path.join __dirname, '../data', file

		writeStream.on 'error', (err) ->
			console.error err.stack   # todo: remove?
			task.reject err
		writeStream.on 'finish', task.resolve

		stringify.pipe writeStream
		for id, schedule of schedules
			delete schedule.start
			delete schedule.days
			stringify.write schedule
		stringify.end()

		return task.promise

