Skip to content

Commit 1f2e04b

Browse files
author
Alan Shaw
authored
docs: update examples (#2319)
Since I was going through these anyway to ensure they work with [0.37](ipfs/js-ipfs#2192) I've updated the examples to use the new [`IPFS.create` constructor](https://github.com/ipfs/js-ipfs#ipfs-constructor) and switched to using the promised API so that we onboard new users to using promises and minimise the disruption caused when ipfs/js-ipfs#1670 bubbles up to here. License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 9d4fa53 commit 1f2e04b

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

index.js

+20-23
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const SPDY = require('libp2p-spdy')
1010
const KadDHT = require('libp2p-kad-dht')
1111
const MPLEX = require('pull-mplex')
1212
const SECIO = require('libp2p-secio')
13-
const assert = require('assert')
1413

1514
/**
1615
* Options for the libp2p bundle
@@ -104,33 +103,31 @@ const libp2pBundle = (opts) => {
104103
})
105104
}
106105

107-
// Now that we have our custom libp2p bundle, let's start up the ipfs node!
108-
const node = new IPFS({
109-
libp2p: libp2pBundle
110-
})
111-
112-
// Listen for the node to start, so we can log out some metrics
113-
node.once('start', (err) => {
114-
assert.ifError(err, 'Should startup without issue')
106+
async function main () {
107+
// Now that we have our custom libp2p bundle, let's start up the ipfs node!
108+
const node = await IPFS.create({
109+
libp2p: libp2pBundle
110+
})
115111

116112
// Lets log out the number of peers we have every 2 seconds
117-
setInterval(() => {
118-
node.swarm.peers((err, peers) => {
119-
if (err) {
120-
console.log('An error occurred trying to check our peers:', err)
121-
process.exit(1)
122-
}
113+
setInterval(async () => {
114+
try {
115+
const peers = await node.swarm.peers()
123116
console.log(`The node now has ${peers.length} peers.`)
124-
})
117+
} catch (err) {
118+
console.log('An error occurred trying to check our peers:', err)
119+
}
125120
}, 2000)
126121

127122
// Log out the bandwidth stats every 4 seconds so we can see how our configuration is doing
128-
setInterval(() => {
129-
node.stats.bw((err, stats) => {
130-
if (err) {
131-
console.log('An error occurred trying to check our stats:', err)
132-
}
123+
setInterval(async () => {
124+
try {
125+
const stats = await node.stats.bw()
133126
console.log(`\nBandwidth Stats: ${JSON.stringify(stats, null, 2)}\n`)
134-
})
127+
} catch (err) {
128+
console.log('An error occurred trying to check our stats:', err)
129+
}
135130
}, 4000)
136-
})
131+
}
132+
133+
main()

0 commit comments

Comments
 (0)