Should a parameter assertion in Javascript address directly when a function returns a Promise?

I've been writing NodeJS code for quite some time now and see various methods implemented by people. My question is weather is considered best practice to have a function that how signature returns Promise

throws directly when a parameter assertion fails.

So, do you think we should do this?

Synchronize

function asyncPromise(param) {
    assert(param, 'Missing required parameter');
    // Param ok
    return Promise.resolve(param);
}

      

Or that?

Promise rejected

function rejectPromise(param) {
    if (typeof param === 'undefined') {
        return Promise.reject(new Error('Missing required parameter'));
    }
    // Param valid
    return Promise.resolve(value);
}

      

My gut is telling me to go in the first direct throw method, since incorrect parameter input should be treated as something that shouldn't continue, however when the function has a Promise return signature, the caller waits for the .catch of the error to be handled and will skip the error if thrown directly.

What is your opinion?

+3


source to share


1 answer


Probably the problem is that you are asking about asynchronous promises but using a synchronous code example.

Correct relevant code should look something like this:

/**
 * @return Promise Resolved when a value is accepted, rejected otherwise.
 * @throw  AssertionException When no parameter is given.
 */
function asyncPromise(param) {
    assert(param, 'Missing required parameter');

    var promise = new Promise(param);
    something.process(promise); //start asynchronous process

    return Promise;
}

//somewhere else while processing
try {
    doSomething();
} catch (Exception err) {
    promise.reject(err);
}

//somewhere else after processing is done
if (success) {
    promise.resolve(result);
}
else {
    promise.reject(error);
}

      

There are two places in this code where an error can occur: A) in asyncPromise()

which is a synchronous error and can be safely handled by throwing an exception, and B) handling in which throwing an Exception will stop processing but return no response to the original function. freezing the code. In such a case, you should reject the promise instead of throwing an exception.



The solution to question A) by rejecting a promise or throwing an exception should depend on the cause of the problem: if due to a problem you cannot create or process a promise (or something related to it), then it should return an exception. If, on the other hand, you can create a promise safely, but the problem occurs when you try to start processing, then it should reject the promise. Also, a reason to abandon a promise is to understand that there is no reason to start the process (for example, when you have already tried and failed).


Also note that the function clearly states that it returns a Promise, but may throw an exception if the parameter is not defined. This way everyone knows what to expect and how to use it.

-1


source







All Articles