Skip to content

Commit 0a2b4e7

Browse files
committed
Async math functions with tests.
1 parent 3678fc4 commit 0a2b4e7

6 files changed

Lines changed: 4758 additions & 0 deletions

File tree

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["es2015"]
3+
}

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,38 @@
11
# async-math
22
Npm package for async math operations.
3+
4+
## Introduction
5+
Stop writing archaic, boring code.
6+
Why use native operators when you can use an npm package?
7+
Why use synchronous arithmetic when you can do so asynchronously?
8+
Why get guaranteed results when you can get the promise of one?
9+
10+
## Usage
11+
12+
### Adding two numbers
13+
```javascript
14+
import { add } from "async-math"
15+
add(8, 4).then(function(sum) { console.log(sum) })
16+
```
17+
Console output should be 12
18+
19+
### Subtracting a number from another
20+
```javascript
21+
import { subtract } from "async-math"
22+
subtract(8, 4).then(function(difference) { console.log(difference) })
23+
```
24+
Console output should be 4
25+
26+
### Multiplying two numbers
27+
```javascript
28+
import { multiply } from "async-math"
29+
multiply(8, 4).then(function(product) { console.log(product) })
30+
```
31+
Console output should be 32
32+
33+
### Divide a number from another
34+
```javascript
35+
import { divide } from "async-math"
36+
divide(8, 4).then(function(quotient) { console.log(quotient) })
37+
```
38+
Console output should be 2

index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
let add = function (num1, num2) {
2+
return new Promise((resolve, reject) => {
3+
try {
4+
const sum = num1 + num2;
5+
setTimeout(function () { resolve(sum); }, 0);
6+
}
7+
catch (e) {
8+
reject(e);
9+
}
10+
});
11+
};
12+
let subtract = function (num1, num2) {
13+
return new Promise((resolve, reject) => {
14+
try {
15+
const difference = num1 - num2;
16+
setTimeout(function () { resolve(difference); }, 0);
17+
}
18+
catch (e) {
19+
reject(e);
20+
}
21+
});
22+
};
23+
let multiply = function (num1, num2) {
24+
return new Promise((resolve, reject) => {
25+
try {
26+
const product = num1 * num2;
27+
setTimeout(function () { resolve(product); }, 0);
28+
}
29+
catch (e) {
30+
reject(e);
31+
}
32+
});
33+
};
34+
let divide = function (num1, num2) {
35+
return new Promise((resolve, reject) => {
36+
try {
37+
const quotient = num1 / num2;
38+
setTimeout(function () { resolve(quotient); }, 0);
39+
}
40+
catch (e) {
41+
reject(e);
42+
}
43+
});
44+
};
45+
let shootMe = function () {
46+
return new Promise((resolve, reject) => {
47+
try {
48+
setTimeout(() => { resolve("bang"); }, 0);
49+
}
50+
catch (e) {
51+
reject(e);
52+
}
53+
});
54+
};
55+
export { add, subtract, multiply, divide, shootMe };

0 commit comments

Comments
 (0)