Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 9fb54d2

Browse files
dirkmcvmx
authored andcommitted
chore: callbacks -> async / await
This is part of the Awesome Endeavour: Async Iterators ipfs/js-ipfs#1670 BREAKING CHANGE: All places in the API that used callbacks are now replaced with async/await
1 parent 4c114bc commit 9fb54d2

File tree

6 files changed

+113
-185
lines changed

6 files changed

+113
-185
lines changed

README.md

+14-25
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,20 @@ const repo = new IPFSRepo('example')
7272

7373
// create a block
7474
const data = new Buffer('hello world')
75-
multihashing(data, 'sha2-256', (err, multihash) => {
76-
if (err) {
77-
throw err
78-
}
79-
80-
const cid = new CID(multihash)
81-
const block = new Block(data, cid)
82-
83-
// create a service
84-
const bs = new BlockService(repo)
85-
86-
// add the block, then retrieve it
87-
bs.put(block, (err) => {
88-
if (err) {
89-
throw err
90-
}
91-
bs.get(cid, (err, b) => {
92-
if (err) {
93-
throw err
94-
}
95-
console.log(block.data.toString() === b.data.toString())
96-
// => true
97-
})
98-
})
99-
})
75+
const multihash = await multihashing(data, 'sha2-256')
76+
77+
const cid = new CID(multihash)
78+
const block = new Block(data, cid)
79+
80+
// create a service
81+
const service = new BlockService(repo)
82+
83+
// add the block, then retrieve it
84+
await service.put(block)
85+
86+
const result = await service.get(cid)
87+
console.log(block.data.toString() === result.data.toString())
88+
// => true
10089
```
10190

10291
### Browser: Browserify, Webpack, other bundlers

package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,18 @@
3434
"chai": "^4.2.0",
3535
"cids": "~0.5.7",
3636
"dirty-chai": "^2.0.1",
37+
"fs-extra": "^8.0.1",
3738
"ipfs-block": "~0.8.0",
38-
"ipfs-repo": "~0.26.2",
39+
"ipfs-repo": "~0.27.0",
3940
"lodash": "^4.17.11",
40-
"multihashing-async": "~0.5.2",
41-
"ncp": "^2.0.0",
42-
"rimraf": "^2.6.3"
41+
"multihashing-async": "~0.7.0"
4342
},
4443
"engines": {
4544
"node": ">=6.0.0",
4645
"npm": ">=3.0.0"
4746
},
4847
"dependencies": {
49-
"async": "^2.6.2"
48+
"streaming-iterables": "^4.0.2"
5049
},
5150
"contributors": [
5251
"David Dias <[email protected]>",

src/index.js

+25-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const asyncMap = require('async/map')
3+
const { map } = require('streaming-iterables')
44

55
/**
66
* BlockService is a hybrid block datastore. It stores data in a local
@@ -54,73 +54,71 @@ class BlockService {
5454
* Put a block to the underlying datastore.
5555
*
5656
* @param {Block} block
57-
* @param {function(Error)} callback
58-
* @returns {void}
57+
* @returns {Promise}
5958
*/
60-
put (block, callback) {
59+
put (block) {
6160
if (this.hasExchange()) {
62-
this._bitswap.put(block, callback)
61+
return this._bitswap.put(block)
6362
} else {
64-
this._repo.blocks.put(block, callback)
63+
return this._repo.blocks.put(block)
6564
}
6665
}
6766

6867
/**
6968
* Put a multiple blocks to the underlying datastore.
7069
*
7170
* @param {Array<Block>} blocks
72-
* @param {function(Error)} callback
73-
* @returns {void}
71+
* @returns {Promise}
7472
*/
75-
putMany (blocks, callback) {
73+
putMany (blocks) {
7674
if (this.hasExchange()) {
77-
this._bitswap.putMany(blocks, callback)
75+
return this._bitswap.putMany(blocks)
7876
} else {
79-
this._repo.blocks.putMany(blocks, callback)
77+
return this._repo.blocks.putMany(blocks)
8078
}
8179
}
8280

8381
/**
8482
* Get a block by cid.
8583
*
8684
* @param {CID} cid
87-
* @param {function(Error, Block)} callback
88-
* @returns {void}
85+
* @returns {Promise<Block>}
8986
*/
90-
get (cid, callback) {
87+
get (cid) {
9188
if (this.hasExchange()) {
92-
this._bitswap.get(cid, callback)
89+
return this._bitswap.get(cid)
9390
} else {
94-
this._repo.blocks.get(cid, callback)
91+
return this._repo.blocks.get(cid)
9592
}
9693
}
9794

9895
/**
9996
* Get multiple blocks back from an array of cids.
10097
*
10198
* @param {Array<CID>} cids
102-
* @param {function(Error, Block)} callback
103-
* @returns {void}
99+
* @returns {Iterator<Block>}
104100
*/
105-
getMany (cids, callback) {
101+
getMany (cids) {
106102
if (!Array.isArray(cids)) {
107-
callback(new Error('first arg must be an array of cids'))
108-
} else if (this.hasExchange()) {
109-
this._bitswap.getMany(cids, callback)
103+
throw new Error('first arg must be an array of cids')
104+
}
105+
106+
if (this.hasExchange()) {
107+
return this._bitswap.getMany(cids)
110108
} else {
111-
asyncMap(cids, (cid, cb) => this._repo.blocks.get(cid, cb), callback)
109+
const getRepoBlocks = map((cid) => this._repo.blocks.get(cid))
110+
return getRepoBlocks(cids)
112111
}
113112
}
114113

115114
/**
116115
* Delete a block from the blockstore.
117116
*
118117
* @param {CID} cid
119-
* @param {function(Error)} callback
120-
* @return {void}
118+
* @returns {Promise}
121119
*/
122-
delete (cid, callback) {
123-
this._repo.blocks.delete(cid, callback)
120+
delete (cid) {
121+
return this._repo.blocks.delete(cid)
124122
}
125123
}
126124

0 commit comments

Comments
 (0)