0

I'm having to call a method with two parameters, but I'm only using the 2nd one. My code looks like this:

  function trapEvent(resolve, reject) {

    event_callback = function (evt) {
      evt.stopPropagation();
      if (allowDefault !== true) {
        evt.preventDefault();
      }
      cancelResolver();
      callback_promise = new Promise()
        .cancellable()
        .then(function () {
          return callback(evt);
        })
        .then(null, function (error) {
          if (!(error instanceof Promise.CancellationError)) {
            canceller();
            reject(error);
          }
        });
    };

    util.startListenTo(target, type, event_callback, useCapture);
  }

  return new Promise(trapEvent, canceller);

As I'm "locking" my event inside a promise, which can only be rejected or cancelled, I'm never using the resolve method, which JSlint rightfully complains about. However, according to MDN es6 Promise spec, I need to provide both resolve and reject in the Promise callback method.

Question:
How can I make this method pass while keeping resolve?

7
  • When you're referencing the function that way, you'll have to use both arguments the way you're doing, and there's nothing wrong with that, just ignore jslint.
    – adeneo
    Aug 10, 2014 at 19:01
  • hm. any other way to do it?
    – frequent
    Aug 10, 2014 at 19:05
  • you can declare as global variable Aug 10, 2014 at 19:10
  • I just did a Google search and found the answer immediately. Aug 10, 2014 at 19:10
  • @cookiemonster - then why not post it ?
    – adeneo
    Aug 10, 2014 at 19:24

2 Answers 2

0

I think the best way to go is update your jshint.json file and configure this to be allowed.

The "unused" property is the one you are looking for. It could be tweaked to either contain the following values:

  • false - don't check for unused variables
  • true - "vars" + check last function param
  • "vars" - skip checking unused function params
  • "strict" - "vars" + check all function params

It seems to me that it currently has been set to "strict". You might want to change that to true or "vars". The latter would be useful for cases where you would not want to call the reject callback and only call the resolve method.

In case you just want to disable jshint in total for just the given line, you could do the following:

function trapEvent(resolve, reject) { //jshint ignore:line
1
  • You are right. What difference one character could make :-)
    – PaulT
    Aug 10, 2014 at 21:10
0

Yeah, for jslint, what you're looking for is unparam -- /*jslint unparam: true*/

Here it is in context. I'm guessing at globals, etc, and I'm assuming you're using the callback_promise somewhere later on. Note the unparam at the end of the first line.

/*jslint white:true, sloppy:true, unparam: true */
/*global allowDefault, cancelResolver, callback, canceller, util, target, type, useCapture */

function trapEvent(resolve, reject) {
    var event_callback, callback_promise;

    event_callback = function (evt) {
      evt.stopPropagation();
      if (allowDefault !== true) {
        evt.preventDefault();
      }
      cancelResolver();
      callback_promise = new Promise()
        .cancellable()
        .then(function () {
          return callback(evt);
        })
        .then(null, function (error) {
          if (!(error instanceof Promise.CancellationError)) {
            canceller();
            reject(error);
          }
        });
    };

    util.startListenTo(target, type, event_callback, useCapture);
    // ... assuming you're using callback_promise later
}

There [used to be] other, slightly hipster ways of working around this, like using the arguments property in a function, where you could leave both parameters out of the function definition entirely, and then pull what's now reject out once you're in the code.

/*jslint white:true, sloppy:true, browser:true */
function trapEvent() {
    var reject = arguments[1];
    reject();
}

var fnTest = function () { window.alert("Used"); };
trapEvent("unused", fnTest);

.. but honestly, that's really hacky. And now that I test that out, recent versions of JSLint (maybe closing the accidental loophole starting July 3rd of last year?) don't accept it any more either, thankfully.

I recommend using unparam, but only in those files where you don't have control of the function signature for whatever reason, as in your use case here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.