Skip to content

Commit aeca7ac

Browse files
author
Lauren Connors
committed
update search function to use async/await syntax
1 parent 8a09eff commit aeca7ac

File tree

1 file changed

+18
-39
lines changed

1 file changed

+18
-39
lines changed

lib/search.js

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,27 @@ let { req, reqJSON, makeCSV, throwError } = require('../utils')
1010
limit
1111
}
1212
*/
13-
module.exports = function(input, params = {}) {
14-
req = req.bind(this)
15-
reqJSON = reqJSON.bind(this)
13+
module.exports = async function(input, params = {}) {
14+
req = req.bind(this);
15+
reqJSON = reqJSON.bind(this);
1616

17-
return new Promise((resolve, reject) => {
18-
if(typeof(input) === 'undefined')
19-
reject(throwError('search()', 'any input'))
17+
if(typeof(input) === 'undefined')
18+
throw throwError('search()', 'any input')
2019

21-
let path = params.lore ? '/lore' : '/search'
20+
let path = params.lore ? '/lore' : '/search'
2221

23-
switch(typeof(input)) {
24-
// GET method
25-
case 'string':
26-
params.indexes = makeCSV(params.indexes)
22+
switch(typeof(input)) {
23+
// GET method
24+
case 'string':
25+
params.indexes = makeCSV(params.indexes);
26+
return req(path, Object.assign(params, {"string": input}));
2727

28-
req(
29-
path,
30-
Object.assign(params, {string: input})
31-
).then((res) => {
32-
resolve(res)
33-
}).catch((err) => {
34-
reject(err)
35-
})
36-
break
28+
// ElasticSearch JSON method
29+
case 'object':
30+
input.indexes = makeCSV(params.indexes);
31+
return reqJSON(path, input);
3732

38-
// ElasticSearch JSON method
39-
case 'object':
40-
input.indexes = makeCSV(params.indexes)
41-
42-
reqJSON(
43-
path,
44-
input
45-
).then((res) => {
46-
resolve(res)
47-
}).catch((err) => {
48-
reject(err)
49-
})
50-
break
51-
52-
default:
53-
reject(Error(`Unexpected input type for search: '${typeof(input)}'`))
54-
break
55-
}
56-
})
33+
default:
34+
throw new Error(`Unexpected input type for search: '${typeof(input)}'`);
35+
}
5736
}

0 commit comments

Comments
 (0)