massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-json / node_modules / es6-promisify / README.md
1 [![Travis CI](https://travis-ci.org/digitaldesignlabs/es6-promisify.svg)](https://travis-ci.org/digitaldesignlabs/es6-promisify)
2
3 # es6-promisify
4
5 Converts callback-based functions to Promise-based functions.
6
7 ## Install
8
9 Install with [npm](https://npmjs.org/package/es6-promisify)
10
11 ```bash
12 npm install --save es6-promisify
13 ```
14
15 ## Example
16
17 ```js
18 "use strict";
19
20 // Declare variables
21 const promisify = require("es6-promisify");
22 const fs = require("fs");
23
24 // Convert the stat function
25 const stat = promisify(fs.stat);
26
27 // Now usable as a promise!
28 stat("example.txt").then(function (stats) {
29     console.log("Got stats", stats);
30 }).catch(function (err) {
31     console.error("Yikes!", err);
32 });
33 ```
34
35 ## Promisify methods
36 ```js
37 "use strict";
38
39 // Declare variables
40 const promisify = require("es6-promisify");
41 const redis = require("redis").createClient(6379, "localhost");
42
43 // Create a promise-based version of send_command
44 const client = promisify(redis.send_command, redis);
45
46 // Send commands to redis and get a promise back
47 client("ping").then(function (pong) {
48     console.log("Got", pong);
49 }).catch(function (err) {
50     console.error("Unexpected error", err);
51 }).then(function () {
52     redis.quit();
53 });
54 ```
55
56 ## Handle callback multiple arguments
57 ```js
58 "use strict";
59
60 // Declare functions
61 function test(cb) {
62     return cb(undefined, 1, 2, 3);
63 }
64
65 // Declare variables
66 const promisify = require("es6-promisify");
67
68 // Create promise-based version of test
69 const single = promisify(test);
70 const multi = promisify(test, {multiArgs: true});
71
72 // Discards additional arguments
73 single().then(function (result) {
74     console.log(result); // 1
75 });
76
77 // Returns all arguments as an array
78 multi().then(function (result) {
79     console.log(result); // [1, 2, 3]
80 });
81 ```
82
83 ### Tests
84 Test with nodeunit
85 ```bash
86 $ npm test
87 ```
88
89 Published under the [MIT License](http://opensource.org/licenses/MIT).