         if request.tab.incognito then "chrome://newtab" else chrome.runtime.getURL newTabUrl
       else
         url
-    if 0 < request.count--
-      TabOperations.openUrlInNewTab request, (tab) => @createTab extend request, {tab, tabId: tab.id}
-  duplicateTab: (request) ->
-    if 0 < request.count--
-      chrome.tabs.duplicate request.tabId, (tab) => @duplicateTab extend request, {tab, tabId: tab.id}
+    TabOperations.openUrlInNewTab request, (tab) -> callback extend request, {tab, tabId: tab.id}
+  duplicateTab: repeatCommand (request, callback) ->
+    chrome.tabs.duplicate request.tabId, (tab) -> callback extend request, {tab, tabId: tab.id}
   moveTabToNewWindow: ({count, tab}) ->
     chrome.tabs.query {currentWindow: true}, (tabs) ->
       activeTabIndex = tab.index

# These are commands which are bound to keystroke which must be handled by the background page. They are
# mapped in commands.coffee.
BackgroundCommands =
  createTab: (request) ->
    request.url ?= do ->
      url = Settings.get "newTabUrl"
      if url == "pages/blank.html"

    chrome.windows.update(tab.windowId, { focused: true })
    chrome.tabs.update(request.id, { selected: true }))

repeatCommand = (command) -> (request) ->
  if 0 < request.count--
    command request, (request) -> (repeatCommand command) request

moveTab = ({count, tab, registryEntry}) ->
  count = -count if registryEntry.command == "moveTabLeft"
  chrome.tabs.getAllInWindow null, (tabs) ->
