Idempotence

What is Idempotence? Given the same inputs with the same output. It is similar to pure function but it is a little bit different.

Not Idempotence function

function notIdempotence(n){
    return Math.random(n);
}

console.log(notIdempotence(1));
the result is continually changed.
  • This is not idempotence.

Idempotence function but not pure

function idempotence_but_not_pure(n){
    return console.log(n);
}
idempotence_but_not_pure(7);
7

This function is idempotence. However, this function is not pure function because console.log().

# Another thing that can be idempotent for example is deleting a user from a database. Idempotence, you see a lot in APIs, like HTTP get requests.

Leave a Reply

Your email address will not be published.

ANOTE.DEV