attempt-statement 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -0
- package/attempt.js +7 -2
- package/package.json +1 -1
- package/test.js +10 -0
package/README.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# attempt-statement
|
|
2
|
+
```javascript
|
|
3
|
+
const attempt = require('attempt-statement')
|
|
4
|
+
attempt(() => {
|
|
5
|
+
consoe.log('I misspelled console')
|
|
6
|
+
}).rescue((error) => {
|
|
7
|
+
console.log('Error: ' + error)
|
|
8
|
+
}).else(() => {
|
|
9
|
+
console.log('No error yay')
|
|
10
|
+
}).ensure(() => {
|
|
11
|
+
console.log('This will always be run')
|
|
12
|
+
}).end()
|
|
13
|
+
// Output:
|
|
14
|
+
// Error: ReferenceError: consoe is not defined
|
|
15
|
+
// This will always be run
|
|
16
|
+
```
|
|
17
|
+
equivalent to:
|
|
18
|
+
```javascript
|
|
19
|
+
let error = false
|
|
20
|
+
try {
|
|
21
|
+
consoe.log('I misspelled console')
|
|
22
|
+
} catch(error) {
|
|
23
|
+
console.log('Error: ' + error)
|
|
24
|
+
error = true
|
|
25
|
+
}
|
|
26
|
+
if (error === false) {
|
|
27
|
+
console.log('No error yay')
|
|
28
|
+
}
|
|
29
|
+
console.log('This will always be run')
|
|
30
|
+
```
|
|
31
|
+
there is no built in way in javascript to achieve the else behavior so attempt-statement is the way to go.
|
package/attempt.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
class
|
|
1
|
+
class Attempt {
|
|
2
2
|
constructor(handler) {
|
|
3
|
-
if (!(this instanceof
|
|
3
|
+
if (!(this instanceof Attempt)) return new Attempt(handler)
|
|
4
4
|
this.trycode = handler
|
|
5
5
|
return this
|
|
6
6
|
}
|
|
@@ -34,5 +34,10 @@ class attempt {
|
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
function attempt(handler) {
|
|
38
|
+
this.______attemptfn = true
|
|
39
|
+
return new Attempt(handler)
|
|
40
|
+
}
|
|
41
|
+
|
|
37
42
|
|
|
38
43
|
module.exports = attempt
|
package/package.json
CHANGED
package/test.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const attempt = require('./attempt')
|
|
2
|
+
attempt(() => {
|
|
3
|
+
consoe.log('I misspelled console')
|
|
4
|
+
}).rescue((error) => {
|
|
5
|
+
console.log('Error: ' + error)
|
|
6
|
+
}).else(() => {
|
|
7
|
+
console.log('No error yay')
|
|
8
|
+
}).ensure(() => {
|
|
9
|
+
console.log('This will always be run')
|
|
10
|
+
}).end()
|