wiki/content/20201126095525-idempotent.md

54 lines
799 B
Markdown
Raw Normal View History

2024-05-06 20:40:05 +00:00
---
id: 48609306-f2e9-4d01-b774-7d3ec34703aa
title: idempotent
---
# Origin
Idem is latin for 'same'.
# Definition
An operation or function, that when called multiple times will always
produce the same result.
# Examples
## Idempotent
``` javascript
console.log(1 * 1 * 1 * 1)
```
``` javascript
const set = new Set()
set.add('blaat')
set.add('blaat')
set.add('blaat')
console.log(set) // { 'blaat' }
```
### Practial examples
- Get, Put & Delete endpoints on a REST api
- Payment operations. Reposting the same form should not result in
multiple charges to the customer.
## Non idempotent
``` javascript
console.log(2 * 2 * 2 * 2)
```
``` javascript
const arr = []
arr.push('blaat')
arr.push('blaat')
arr.push('blaat')
console.log(arr) // [ 'blaat', 'blaat', 'blaat' ]
```