Skip to content

Commit

Permalink
Create Sketch-PipelineOperator-WasOkayTest.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey authored Mar 10, 2024
1 parent f55b9a2 commit cb88b46
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Pwsh/Operators/Sketch-PipelineOperator-WasOkayTest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
```ps1
function HandleStuff {
process {
if($_ -le 5) {
write-verbose 'was <= ?' -Verbose
write-error 'invalid range' }
}
}
function Test-WasOk { 'πŸ‘' }
function GetFallback { 400 }
```
### Using `||` as a fallback ?

```sh
Can it run fallback when there's an error?
0,3,10 | HandleStuff || GetFallback
```
output
```
VERBOSE: was <= ?
HandleStuff: invalid range
VERBOSE: was <= ?
HandleStuff: invalid range
```
`GetFallback` is never called because the final value in the pipeline was not an error
### Using `&&` to run command2 only if command1 is successful?
0,3,10 | HandleStuff && Test-WasOk
```ps1
> 100 | HandleStuff && Test-WasOk
# πŸ‘
> 3 | HandleStuff && Test-WasOk
VERBOSE: was <= ?
HandleStuff: invalid range
πŸ‘
```
```ps1
3, 10 | HandleStuff && Test-WasOk
VERBOSE: was <= ?
HandleStuff: invalid range
πŸ‘
Pwsh 7.4.1> [22] πŸ’
10, 3 | HandleStuff && Test-WasOk
VERBOSE: was <= ?
HandleStuff: invalid range
πŸ‘
Pwsh 7.4.1> [23] πŸ’
10, 3, 99 | HandleStuff && Test-WasOk
VERBOSE: was <= ?
HandleStuff: invalid range
πŸ‘
```

0 comments on commit cb88b46

Please sign in to comment.