You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 12, 2024. It is now read-only.
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>
Copy file name to clipboardExpand all lines: README.md
+91-49Lines changed: 91 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -355,35 +355,55 @@ Start listening for connections with other IPFS nodes on the network. In most ca
355
355
This method is asynchronous. There are several ways to be notified when the node has finished starting:
356
356
357
357
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
+
awaitnode.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:thismethodwill**not**returnapromiseifyouuseacallbackfunction).
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
+
returnconsole.error('Node failed to start!', error)
385
+
}
386
+
console.log('Node started!')
387
+
})
388
+
})
389
+
```
390
+
359
391
3. You can listen for the [`start`event](#events).
360
392
361
-
```js
362
-
const node = new IPFS({ start:false })
393
+
```js
394
+
const node = new IPFS({ start: false })
363
395
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
+
})
380
400
381
-
// OR use events:
382
-
node.on('error', error=>console.error('Something went terribly wrong!', error))
@@ -392,33 +412,55 @@ Close and stop listening for connections with other IPFS nodes, then release acc
392
412
This method is asynchronous. There are several ways to be notified when the node has completely stopped:
393
413
394
414
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:thismethodwill**not**returnapromiseifyouuseacallbackfunction).
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
+
returnconsole.error('Node failed to stop cleanly!', error)
442
+
}
443
+
console.log('Node stopped!')
444
+
})
445
+
})
446
+
```
447
+
396
448
3. You can listen for the [`stop` event](#events).
397
449
398
-
```js
399
-
constnode=newIPFS()
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()
416
452
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)
0 commit comments