     @pattern = ''
     @emit('cleared')
 
-  search: (pattern, paths, onlyRunIfChanged = false)->
-    if onlyRunIfChanged and pattern? and paths? and pattern == @pattern and _.isEqual(paths, @searchedPaths)
-      # Returning a promise that is already resolved (i.e. Q()) is problematic.
-      # Produces undefined behavior. (Maybe a Q bug?) Example:
-      #
-      # prom = @search('items', '', true).then =>
-      #   @replace('items', 'blah')
-      #
-      # prom can (and sometimes does) resolve _before_ the @replace promise!
-      deferred = Q.defer()
-      setImmediate -> deferred.resolve()
-      deferred.promise
+  search: (pattern, paths, onlyRunIfChanged = false) ->
+    return Q() if onlyRunIfChanged and pattern? and paths? and pattern == @pattern and _.isEqual(paths, @searchedPaths)
 
     @clear()
     @active = true

    promise = atom.project.scan @regex, {paths, onPathsSearched}, (result) =>
      @setResult(result.filePath, result.matches)

    promise.done => @emit('finished-searching')

    @emit('search', promise)

    promise

  replace: (pattern, replacementText, paths) ->
    regex = @getRegex(pattern)

        replacements += result.replacements
      @emit('path-replaced', result)

    @emit('replace', promise)
    promise.then =>
      @clear()
      @emit('finished-replacing', {pathsReplaced, replacements})

  toggleUseRegex: ->
    @useRegex = not @useRegex
