Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 80ae33e

Browse files
davidgilbertsonalanshaw
authored andcommitted
docs: use async/await in 101 example (#1491) (#1495)
Replaces the async npm package with vanilla JavaScript. License: MIT Signed-off-by: David Gilbertson <[email protected]>
1 parent 57f977f commit 80ae33e

File tree

4 files changed

+67
-65
lines changed

4 files changed

+67
-65
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ This project is available through [npm](https://www.npmjs.com/). To install run
9797
> npm install ipfs --save
9898
```
9999

100-
Requires npm@3 and node@6 or above, tested on OSX & Linux, expected to work on Windows.
100+
We support both the Current and Active LTS versions of Node.js. Please see [nodejs.org](https://nodejs.org/) for what these currently are.
101+
102+
This project is tested on OSX & Linux, expected to work on Windows.
101103

102104
### Use in Node.js
103105

examples/ipfs-101/1.js

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
'use strict'
22

3-
const series = require('async/series')
43
const IPFS = require('ipfs')
54

65
const node = new IPFS()
7-
let fileMultihash
8-
9-
series([
10-
(cb) => node.on('ready', cb),
11-
(cb) => node.version((err, version) => {
12-
if (err) { return cb(err) }
13-
console.log('Version:', version.version)
14-
cb()
15-
}),
16-
(cb) => node.files.add({
6+
7+
node.on('ready', async () => {
8+
const version = await node.version()
9+
10+
console.log('Version:', version.version)
11+
12+
const filesAdded = await node.files.add({
1713
path: 'hello.txt',
1814
content: Buffer.from('Hello World 101')
19-
}, (err, filesAdded) => {
20-
if (err) { return cb(err) }
21-
22-
console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)
23-
fileMultihash = filesAdded[0].hash
24-
cb()
25-
}),
26-
(cb) => node.files.cat(fileMultihash, (err, data) => {
27-
if (err) { return cb(err) }
28-
29-
console.log('\nFile content:')
30-
process.stdout.write(data)
3115
})
32-
])
16+
17+
console.log('Added file:', filesAdded[0].path, filesAdded[0].hash)
18+
19+
const fileBuffer = await node.files.cat(filesAdded[0].hash)
20+
21+
console.log('Added file contents:', fileBuffer.toString())
22+
})

examples/ipfs-101/README.md

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,91 @@
11
# IPFS 101, spawn a node and add a file to the IPFS network
22

3-
In this tutorial, we go through spawning an IPFS node, adding a file and cat'ing the file multihash locally and throught the gateway.
3+
In this tutorial, we go through spawning an IPFS node, adding a file and cat'ing the file multihash locally and through the gateway.
44

5-
You can find a complete version of this tutorial in [1.js](./1.js). For this tutorial, you need to install the following dependencies: `ipfs` and `async` using `npm install ipfs async`.
5+
You can find a complete version of this tutorial in [1.js](./1.js). For this tutorial, you need to install `ipfs` using `npm install ipfs`.
66

77
Creating an IPFS instance can be done in one line, after requiring the module, you simply have to:
88

9-
```JavaScript
9+
```js
1010
const IPFS = require('ipfs')
1111

1212
const node = new IPFS()
1313
```
1414

15-
We can listen for the `ready` event to learn when the node is ready to be used. In this part, we start using `async/series` to help us manage the async flow. As a test, we are going to check the version of the node.
15+
We can listen for the `ready` event to learn when the node is ready to be used. Within the ready event, we'll use `async`/`await` to help us manage the async flow.
1616

17-
```JavaScript
17+
As a test, we are going to check the version of the node.
18+
19+
```js
1820
const IPFS = require('ipfs')
1921

2022
const node = new IPFS()
2123

22-
series([
23-
(cb) => node.on('ready', cb),
24-
(cb) => node.version((err, version) => {
25-
if (err) { return cb(err) }
26-
console.log('Version:', version.version)
27-
cb()
28-
})
29-
])
24+
node.on('ready', async () => {
25+
const version = await node.version()
26+
27+
console.log('Version:', version.version)
28+
})
3029
```
3130

31+
(If you prefer not to use `async`/`await`, you can instead use `.then()` as you would with any promise,
32+
or pass an [error-first callback](https://nodejs.org/api/errors.html#errors_error_first_callbacks), e.g. `node.version((err, version) => { ... })`)
33+
3234
Running the code above gets you:
3335

3436
```bash
3537
> node 1.js
36-
IPFS Version: 0.25.0
38+
Version: 0.31.2
3739
```
3840

39-
Now lets make it more interesting and add a file to IPFS. We can do it by adding another async call to the series that uses the `node.files.add` call. You can learn about IPFS API for files at [interface-ipfs-core](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md).
40-
41-
```JavaScript
42-
// Create the File to add, a file consists of a path + content. More details on
43-
// https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md
44-
(cb) => node.files.add({
45-
path: 'hello.txt',
46-
content: Buffer.from('Hello World')
47-
}, (err, filesAdded) => {
48-
if (err) { return cb(err) }
49-
50-
// Once the file is added, we get back an object containing the path, the
51-
// multihash and the sie of the file
52-
console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)
53-
fileMultihash = filesAdded[0].hash
54-
cb()
41+
Now let's make it more interesting and add a file to IPFS using `node.files.add`. A file consists of a path and content.
42+
43+
You can learn about the IPFS File API at [interface-ipfs-core](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/FILES.md).
44+
45+
```js
46+
node.on('ready', async () => {
47+
const version = await node.version()
48+
49+
console.log('Version:', version.version)
50+
51+
const filesAdded = await node.files.add({
52+
path: 'hello.txt',
53+
content: Buffer.from('Hello World 101')
54+
})
55+
56+
console.log('Added file:', filesAdded[0].path, filesAdded[0].hash)
5557
})
5658
```
5759

58-
If you avoid calling that last `cb()`, the program won't exit enabling you to go to an IPFS Gateway and load the printed hash from a gateway. Go ahead and try it!
60+
You can now go to an IPFS Gateway and load the printed hash from a gateway. Go ahead and try it!
5961

6062
```bash
6163
> node 1.js
62-
Version: 0.25.0
64+
Version: 0.31.2
6365

6466
Added file: hello.txt QmXgZAUWd8yo4tvjBETqzUy3wLx5YRzuDwUQnBwRGrAmAo
6567
# Copy that hash and load it on the gateway, here is a prefiled url:
6668
# https://ipfs.io/ipfs/QmXgZAUWd8yo4tvjBETqzUy3wLx5YRzuDwUQnBwRGrAmAo
6769
```
6870

69-
The last step of this tutorial is retrieving the file back using the `cat` 😺 call. Add another step on the series chain that does the following:
71+
The last step of this tutorial is retrieving the file back using the `cat` 😺 call.
72+
73+
```js
74+
node.on('ready', async () => {
75+
const version = await node.version()
76+
77+
console.log('Version:', version.version)
78+
79+
const filesAdded = await node.files.add({
80+
path: 'hello.txt',
81+
content: Buffer.from('Hello World 101')
82+
})
83+
84+
console.log('Added file:', filesAdded[0].path, filesAdded[0].hash)
7085

71-
```JavaScript
72-
(cb) => node.files.cat(fileMultihash, (err, data) => {
73-
if (err) { return cb(err) }
86+
const fileBuffer = await node.files.cat(filesAdded[0].hash)
7487

75-
console.log('\nFile content:')
76-
// print the file to the terminal and then exit the program
77-
process.stdout.write(data)
88+
console.log('Added file contents:', fileBuffer.toString())
7889
})
7990
```
8091

examples/ipfs-101/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"author": "David Dias <[email protected]>",
1010
"license": "MIT",
1111
"dependencies": {
12-
"async": "^2.6.0",
1312
"ipfs": "file:../../"
1413
}
1514
}

0 commit comments

Comments
 (0)