0

I have multiple function with the same default parameters for all of it.

Is there somehow to add default parameters for all of it without copying and paste?

// 1st function
export function modify(id, values, failCB = (err) => error("🚫 Error"), successCB = () => success("✌ Success")) {
    //...
    if (err) {
        failCB(err.message);
    } else {
        successCB()
    }
}

// 2nd function
export function remove(id, failCB = (err) => error("🚫 Error"), successCB = () => success("✌ Success")) {
    //...
    if (err) {
        failCB(err.message);
    } else {
        successCB()
    }
};

3 Answers 3

1

You can declare variables that store the default value of the parameters and refer those variables instead:

const defaultCbIfFailed = err => error("🚫 Error");
const defaultCbIfSuccess = () => success("✌ Success");

//1st function
export function modify(id, values, failCB = defaultCbIfFailed, successCB = defaultCbIfSuccess) {
    //...
    if (err) {
        failCB(err.message);
    } else {
        successCB()
    }
}

// 2nd function
export function remove(id, failCB = defaultCbIfFailed, successCB = defaultCbIfSuccess) {
    //...
    if (err) {
        failCB(err.message);
    } else {
        successCB()
    }
};
Sign up to request clarification or add additional context in comments.

Comments

1

Use function declarations for your callbacks and pass references to those:

// 1st function
export function modify(id, values, failCB, successCB) {
    //...
    if (err) {
        failCB(err.message);
    } else {
        successCB()
    }
}

// 2nd function
export function remove(id, values, failCB, successCB) {
    //...
    if (err) {
        failCB(err.message);
    } else {
        successCB()
    }
};

function failCB = (err) => error("🚫 Error")
function successCB = () => success("✌ Success")

Comments

0

I knew what Shrihan answered. What I actually want is something like this

removeSubject.parameters = failCB;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.