   notifyWhenDone: (cb) ->
     sitemapThis = this
     process.nextTick =>
-      if sitemapThis.flushed && sitemapThis.fileFlushed
-        console.log "!! sitemap done #{sitemapThis.fileName}, #{sitemapThis.urlCount} urls"
-        cb(null, true)
-      else
-        sitemapThis.gzipper.on 'end', =>
-          sitemapThis.file.on 'end', =>
-            console.log "!! sitemap done #{sitemapThis.fileName}, #{sitemapThis.urlCount} urls"
-            cb(null, true)
+      async.until(
+        -> sitemapThis.flushed && sitemapThis.fileFlushed
+        (untilCb) ->
+          setTimeout(untilCb, 1000)
+        (err) ->
+          console.log "!! sitemap done #{sitemapThis.fileName}, #{sitemapThis.urlCount} urls, err: #{err}"
+          cb(err, true)
+      )
 
   close: ->
     return if @closed


    @gzipper.on 'end', =>
      sitemapThis.flushed = true
    @file.on 'end', =>
      sitemapThis.fileFlushed = true
    @stream.pipe(@gzipper).pipe(@file)
    @stream.emit 'data', config.sitemapHeader

config = require './config'
fs = require 'fs'
Stream = require 'stream'
async = require 'async'

escapeXmlValue = (str) ->
  str.replace(/&/g, '&amp;')
