From 1f2e04bcff390a8b995ad731987a9281c6bc798c Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 2 Aug 2019 17:24:13 +0100 Subject: [PATCH] docs: update examples (#2319) Since I was going through these anyway to ensure they work with [0.37](https://github.com/ipfs/js-ipfs/issues/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 https://github.com/ipfs/js-ipfs/issues/1670 bubbles up to here. License: MIT Signed-off-by: Alan Shaw --- index.js | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index b2244b5..4b85fa2 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,6 @@ const SPDY = require('libp2p-spdy') const KadDHT = require('libp2p-kad-dht') const MPLEX = require('pull-mplex') const SECIO = require('libp2p-secio') -const assert = require('assert') /** * Options for the libp2p bundle @@ -104,33 +103,31 @@ const libp2pBundle = (opts) => { }) } -// Now that we have our custom libp2p bundle, let's start up the ipfs node! -const node = new IPFS({ - libp2p: libp2pBundle -}) - -// Listen for the node to start, so we can log out some metrics -node.once('start', (err) => { - assert.ifError(err, 'Should startup without issue') +async function main () { + // Now that we have our custom libp2p bundle, let's start up the ipfs node! + const node = await IPFS.create({ + libp2p: libp2pBundle + }) // Lets log out the number of peers we have every 2 seconds - setInterval(() => { - node.swarm.peers((err, peers) => { - if (err) { - console.log('An error occurred trying to check our peers:', err) - process.exit(1) - } + setInterval(async () => { + try { + const peers = await node.swarm.peers() console.log(`The node now has ${peers.length} peers.`) - }) + } catch (err) { + console.log('An error occurred trying to check our peers:', err) + } }, 2000) // Log out the bandwidth stats every 4 seconds so we can see how our configuration is doing - setInterval(() => { - node.stats.bw((err, stats) => { - if (err) { - console.log('An error occurred trying to check our stats:', err) - } + setInterval(async () => { + try { + const stats = await node.stats.bw() console.log(`\nBandwidth Stats: ${JSON.stringify(stats, null, 2)}\n`) - }) + } catch (err) { + console.log('An error occurred trying to check our stats:', err) + } }, 4000) -}) +} + +main()