mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-23 03:56:23 +00:00
54 lines
799 B
Markdown
54 lines
799 B
Markdown
|
---
|
||
|
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' ]
|
||
|
```
|