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

Commit d338f6a

Browse files
authored
docs: better start and stop examples (#1521)
This PR adds separate examples for each of the different methods of starting/stopping a node (promises, callbacks, events) instead of a single confusing example with all the methods in one code snippet. This makes for easier copy/paste. Also switches promise method to use async/await syntax. License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent dbf17a3 commit d338f6a

File tree

1 file changed

+91
-49
lines changed

1 file changed

+91
-49
lines changed

README.md

Lines changed: 91 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -355,35 +355,55 @@ Start listening for connections with other IPFS nodes on the network. In most ca
355355
This method is asynchronous. There are several ways to be notified when the node has finished starting:
356356

357357
1. If you call `node.start()` with no arguments, it returns a promise.
358-
2. If you pass a function as the final argument, it will be called when the node is started. *(Note: this method will **not** return a promise if you use a callback function.)*
358+
359+
```js
360+
const node = new IPFS({ start: false })
361+
362+
node.on('ready', async () => {
363+
console.log('Node is ready to use!')
364+
365+
try {
366+
await node.start()
367+
console.log('Node started!')
368+
} catch (error) {
369+
console.error('Node failed to start!', error)
370+
}
371+
})
372+
```
373+
374+
2. If you pass a function as the final argument, it will be called when the node is started (Note: this method will **not** return a promise if you use a callback function).
375+
376+
```js
377+
const node = new IPFS({ start: false })
378+
379+
node.on('ready', () => {
380+
console.log('Node is ready to use!')
381+
382+
node.start(error => {
383+
if (error) {
384+
return console.error('Node failed to start!', error)
385+
}
386+
console.log('Node started!')
387+
})
388+
})
389+
```
390+
359391
3. You can listen for the [`start` event](#events).
360392

361-
```js
362-
const node = new IPFS({ start: false })
393+
```js
394+
const node = new IPFS({ start: false })
363395
364-
node.on('ready', () => {
365-
console.log('Node is ready to use!')
366-
367-
// Use a promise:
368-
node.start()
369-
.then(() => console.log('Node started!'))
370-
.catch(error => console.error('Node failed to start!', error))
371-
372-
// OR use a callback:
373-
node.start(error => {
374-
if (error) {
375-
console.error('Node failed to start!', error)
376-
return
377-
}
378-
console.log('Node started!')
379-
})
396+
node.on('ready', () => {
397+
console.log('Node is ready to use!')
398+
node.start()
399+
})
380400
381-
// OR use events:
382-
node.on('error', error => console.error('Something went terribly wrong!', error))
383-
node.on('start', () => console.log('Node started!'))
384-
node.start()
385-
})
386-
```
401+
node.on('error', error => {
402+
console.error('Something went terribly wrong!', error)
403+
})
404+
405+
node.on('start', () => console.log('Node started!'))
406+
```
387407

388408
#### `node.stop([callback])`
389409

@@ -392,33 +412,55 @@ Close and stop listening for connections with other IPFS nodes, then release acc
392412
This method is asynchronous. There are several ways to be notified when the node has completely stopped:
393413

394414
1. If you call `node.stop()` with no arguments, it returns a promise.
395-
2. If you pass a function as the final argument, it will be called when the node is stopped. *(Note: this method will **not** return a promise if you use a callback function.)*
415+
416+
```js
417+
const node = new IPFS()
418+
419+
node.on('ready', async () => {
420+
console.log('Node is ready to use!')
421+
422+
try {
423+
await node.stop()
424+
console.log('Node stopped!')
425+
} catch (error) {
426+
console.error('Node failed to stop cleanly!', error)
427+
}
428+
})
429+
```
430+
431+
2. If you pass a function as the final argument, it will be called when the node is stopped (Note: this method will **not** return a promise if you use a callback function).
432+
433+
```js
434+
const node = new IPFS()
435+
436+
node.on('ready', () => {
437+
console.log('Node is ready to use!')
438+
439+
node.stop(error => {
440+
if (error) {
441+
return console.error('Node failed to stop cleanly!', error)
442+
}
443+
console.log('Node stopped!')
444+
})
445+
})
446+
```
447+
396448
3. You can listen for the [`stop` event](#events).
397449

398-
```js
399-
const node = new IPFS()
400-
node.on('ready', () => {
401-
console.log('Node is ready to use!')
402-
403-
// Stop with a promise:
404-
node.stop()
405-
.then(() => console.log('Node stopped!'))
406-
.catch(error => console.error('Node failed to stop cleanly!', error))
407-
408-
// OR use a callback:
409-
node.stop(error => {
410-
if (error) {
411-
console.error('Node failed to stop cleanly!', error)
412-
return
413-
}
414-
console.log('Node stopped!')
415-
})
450+
```js
451+
const node = new IPFS()
416452

417-
// OR use events:
418-
node.on('error', error => console.error('Something went terribly wrong!', error))
419-
node.stop()
420-
})
421-
```
453+
node.on('ready', () => {
454+
console.log('Node is ready to use!')
455+
node.stop()
456+
})
457+
458+
node.on('error', error => {
459+
console.error('Something went terribly wrong!', error)
460+
})
461+
462+
node.on('stop', () => console.log('Node stopped!'))
463+
```
422464

423465
#### Core API
424466

0 commit comments

Comments
 (0)