     editor
 
 setUpEditor = (id, html, onMutate) ->
-  whenPresent (-> document.getElementById id),
-    ((elem) -> getEditorFor(elem, onMutate).writeHtml html, true),
-    ((err) -> console.error "Could not write after 10,000 attempts:", id, html),
-    10000
+  whenPresent((-> document.getElementById id), 10000)
+    .then ((elem) -> getEditorFor(elem, onMutate).writeHtml html, true),
+          ((err)  -> console.error "Could not write after 10,000 attempts:", id, html)
 
 refreshDocList = -> sync.listDocs().then (docs) ->
   app.ports.listDocs.send docs

    reader.readAsText file


whenPresent = (getElem, onSuccess, onError, attemptsRemaining = 10000) ->
  elem = getElem()

  if elem?
    onSuccess elem
  else if attemptsRemaining
    requestAnimationFrame ->
      whenPresent getElem, onSuccess, onError, attemptsRemaining - 1
  else
    onError()

editors = new WeakMap()
mutationObserverOptions =

  refreshDocList()

app.ports.downloadDoc.subscribe ({filename, contentType}) ->
  whenPresent((-> document.getElementById "document-page"), 10000)
    .then ((elem) ->
      clone = document.getElementById("document-page").cloneNode true

      for editable in clone.querySelectorAll("[contentEditable=true]")
