├── .editorconfig
├── .eslintrc.json
├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── lib
└── index.js
├── package-lock.json
├── package.json
└── test
└── index.test.js
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | indent_style = tab
5 | indent_size = 2
6 | end_of_line = lf
7 | charset = utf-8
8 | trim_trailing_whitespace = true
9 | insert_final_newline = true
10 |
11 | [{package.json,*.yml,*.md}]
12 | indent_style = space
13 | indent_size = 2
14 |
--------------------------------------------------------------------------------
/.eslintrc.json:
--------------------------------------------------------------------------------
1 | {
2 | "root": true,
3 | "extends": "eslint:recommended",
4 | "env": {
5 | "node": true
6 | },
7 | "parserOptions": {
8 | "ecmaVersion": 8,
9 | "ecmaFeatures": {
10 | "experimentalObjectRestSpread": true
11 | }
12 | },
13 | "rules": {
14 | "no-undef": 2,
15 | "no-unused-vars": 2,
16 | "eqeqeq": 2,
17 | "quotes": [
18 | 0,
19 | "single"
20 | ],
21 | "strict": 0,
22 | "no-use-before-define": 0,
23 | "curly": [
24 | 2,
25 | "multi-line"
26 | ],
27 | "no-shadow": 0,
28 | "new-cap": 0,
29 | "eol-last": 0,
30 | "no-underscore-dangle": 0,
31 | "no-console": 2,
32 | "space-before-function-paren": [
33 | 2,
34 | {
35 | "anonymous": "always",
36 | "named": "never"
37 | }
38 | ],
39 | "space-before-blocks": [
40 | 2,
41 | {
42 | "functions": "always",
43 | "keywords": "always",
44 | "classes": "always"
45 | }
46 | ],
47 | "keyword-spacing": [
48 | 2,
49 | {
50 | "before": true,
51 | "after": true
52 | }
53 | ]
54 | },
55 | "globals": {
56 | "Promise": true,
57 | "HttpError": true
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Dependencies
2 | node_modules
3 |
4 | # Build
5 | dist
6 |
7 | # Logs
8 | npm-debug.log
9 |
10 | # Coverage
11 | coverage
12 | .nyc_output
13 |
14 | # Editors
15 | .vscode
16 | .idea
17 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - "8"
4 | - "10"
5 | after_success:
6 | - npm run coverage
7 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017-2018 MONO JS
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |

2 |
3 | > Node.js utils to deal with async/await.
4 |
5 | [](https://www.npmjs.com/package/mono-utils)
6 | [](https://travis-ci.org/mono-js/mono-utils)
7 | [](https://codecov.io/mono-js/mono-utils)
8 | [](https://github.com/mono-js/mono-utils/blob/master/LICENSE)
9 |
10 |
11 | ## Installation
12 |
13 |
14 | ```bash
15 | npm install --save mono-utils
16 | ```
17 |
18 | ## Usage
19 |
20 | **INFO:** You need `node` >= `8.0.0` to use mono-utils since it uses native `async/await`
21 |
22 | ```js
23 | const { ok, cb, waitFor, ... } = require('mono-utils')
24 | ```
25 |
26 | ## Utils
27 |
28 | - [ok](#ok)
29 | - [cb](#cb)
30 | - [waitFor](#waitfor)
31 | - [waitForEvent](#waitforevent)
32 | - [asyncObject](#asyncobject)
33 | - [asyncMap](#asyncmap)
34 | - [asyncForEach](#asyncforeach)
35 |
36 | ### ok
37 |
38 | Waits for the value of the `Promise` and returns its value. If the promise throws an `Error`, returns `undefined`.
39 |
40 | ```js
41 | ok(promise: Object): Promise
42 | ```
43 |
44 | Example:
45 |
46 | ```js
47 | const { ok } = require('mono-core/utils')
48 | const { readFile } = require('fs-extra')
49 |
50 | // readFile sends back a Promise since we use fs-extra
51 | const file = await ok(readFile('./my-file.txt', 'utf-8'))
52 |
53 | if (file) console.log('File found:', file)
54 | ```
55 |
56 | ### cb
57 |
58 | Calls a function Node style function as first argument `function (err, result)`, all the others arguments will be given to the function. Waits for the callback result, throws an `Error` if `err` is truthy.
59 |
60 | ```js
61 | cb(fn: Function, ...args: any[]): Promise
62 | ```
63 |
64 | Example:
65 |
66 | ```js
67 | const { ok } = require('mono-core/utils')
68 | const fs = require('fs')
69 |
70 | try {
71 | const file = await cb(fs.readFile, '/path/to/my/file.txt', 'utf-8')
72 | } catch (err) {
73 | // Could not read file
74 | }
75 | ```
76 |
77 | ### waitFor
78 |
79 | Waits for `ms` milliseconds to pass, use `setTimeout` under the hood.
80 |
81 | ```js
82 | waitFor(ms: number): Promise
83 | ```
84 |
85 | Example:
86 |
87 | ```js
88 | const { waitFor } = require('mono-core/utils')
89 |
90 | await waitFor(1000) // wait for 1s
91 | ```
92 |
93 | ### waitForEvent
94 |
95 | Waits for emitter to emit an eventName event.
96 |
97 | ```js
98 | waitForEvent(emitter: EventEmitter, eventName: string, timeout: number = -1): Promise
99 | ```
100 |
101 | Example:
102 |
103 | ```js
104 | const { waitFor } = require('mono-core/utils')
105 |
106 | await waitForEvent(sever, 'listen')
107 | ```
108 |
109 | ### asyncObject
110 |
111 | Waits for all Promises in the keys of obj to resolve.
112 |
113 | ```js
114 | asyncObject(obj: Object): Promise