title: Cordova: How to write native plugins that can repeatedly invoke a Javascript callback?
body: <p>When developing Cordova plugins, all of the tutorials I have found go something like this:</p>
  
  <p><strong>File: AwesomePlugin.js</strong></p>
  
  <pre><code>var AwesomePlugin = {
    kungfuGripAction = function(target, successCallback, failureCallback) {
      return cordova.exec(
        successCallback,
        failureCallback,
        'AwesomePluginClass',
        'kungfuGripAction',
        [target]
      );
    }
  };
  
  module.exports = AwesomePlugin;
  </code></pre>
  
  <p><strong>File: AwesomePluginClass.java</strong></p>
  
  <pre><code>@Override
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
      if (ACTION_KUNGFU_GRIP.equals(action)) {
          JSONObject target = args.getJSONObject(0);
          if (gripTarget(target)) {
              callbackContext.success("Target successfully gripped.");
              return true;
          } else {
              callbackContext.error("Could not grip target.");
              return false;
          }
      }
  
      Log.d(LOG_TAG, "INVALID ACTION! " + action);
      callbackContext.error("Invalid action: " + action);
      return false;
  }
  </code></pre>
  
  <p><strong>File: clientCode.js</strong></p>
  
  <pre><code>AwesomePlugin.kungfuGripAction(cobraEnemy, function(ok) { }, function(err) { });
  </code></pre>
  
  <p>In the above code, the callbacks can only be called once and are then disposed. If you attempt to call the <code>.success()</code> or <code>.error()</code> method of the callback context object, it will not work and you will get a log message:</p>
  
  <pre><code>Attempted to send a second callback for ID: AwesomePlugin2982699494&lt;BR&gt;W/CordovaPlugin(976) Result was: "Target successfully gripped."
  </code></pre>
  
  <p>It seems like it is not possible to write a method with a callback that can be called repeatedly seeing as <code>.success()</code> and <code>.error()</code> are the only documented ways to invoke a callback from within native plugin code. While this is mostly what we want, there are times when we want to have the plugin execute a callback repeatedly. For example:</p>
  
  <pre><code>AwesomePlugin.kungfuGripAction(cobraEnemy, function(ok) {
    // After successful grip, punch repeatedly and update life meter.
    AwesomePlugin.punchRepeatedly(cobraEnemy, function(hits) {
      updateLifeMeter(cobraEnemy, hits);
    }, function(err) { });
  }, function(err) { });
  </code></pre>
  
  <p><code>AwesomePlugin.punchRepeatedly()</code> above will execute repeatedly (maybe in a separate thread) and call <code>function(hits)</code> with each successful execution. If implemented in the de-facto way (using single-use callbacks), you have to either use a loop (which is bad as it is non-async) or tail-call <code>AwesomePlugin.punchRepeatedly()</code> in the callback (error-prone).</p>
  
  <p>What would be the correct way to implement <code>punchRepeatedly()</code> in native code so that it is able register the callback once and then execute it repeatedly?</p>