Skip to content

Commit 775d08a

Browse files
committed
Using promises to open new files
1 parent 1e9ea5a commit 775d08a

1 file changed

Lines changed: 5 additions & 17 deletions

File tree

src/data-search.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// here we will convert the search.sh script into JS
44
import { format, sub } from 'date-fns';
5-
import fs from 'fs';
5+
import fs from 'fs/promises';
66
import { download, search, sendOutput } from './cli.js';
77

88
const CATEGORY_ID = 'alzheimers-disease';
@@ -23,10 +23,7 @@ const OUTPUT_FILE = `${DATA_DIRECTORY}/${CATEGORY_ID}.json`;
2323

2424
// Getting all latest articles from BiorXiv
2525
console.log(`Fetching from ${BIORXIV_SOURCE} between ${START_DATE} and ${END_DATE}`);
26-
fs.open(BIORXIV_FILE, 'w', function (err, file) { // consider changing the callback function if needed
27-
if (err) throw err;
28-
console.log('Saved!');
29-
});
26+
fs.open(BIORXIV_FILE, 'w');
3027
const bioOptions = {
3128
source: BIORXIV_SOURCE,
3229
output: BIORXIV_FILE
@@ -35,10 +32,7 @@ const bioData = await download(START_DATE, END_DATE, bioOptions);
3532

3633
// Getting all latest articles from MedrXiv
3734
console.log(`Fetching from ${MEDRXIV_SOURCE} between ${START_DATE} and ${END_DATE}`);
38-
fs.open(MEDRXIV_FILE, 'w', function (err, file) {
39-
if (err) throw err;
40-
console.log('Saved!');
41-
});
35+
fs.open(MEDRXIV_FILE, 'w');
4236
const medOptions = {
4337
source: MEDRXIV_SOURCE,
4438
output: MEDRXIV_FILE
@@ -47,10 +41,7 @@ const medData = await download(START_DATE, END_DATE, medOptions);
4741

4842
// Creating a JSON with all the results, both sources combined
4943
console.log('Combining results...');
50-
fs.open(COMBINED_FILE, 'w', function (err, file) {
51-
if (err) throw err;
52-
console.log('Saved!');
53-
});
44+
fs.open(COMBINED_FILE, 'w');
5445
const combinedData = bioData.concat(medData);
5546
const combinedOptions = {
5647
output: COMBINED_FILE
@@ -59,10 +50,7 @@ await sendOutput(combinedData, combinedOptions);
5950

6051
// Search for the QUERY keyword in all the downloaded articles & compile the related articles
6152
const QUERY = 'alzheimer';
62-
fs.open(OUTPUT_FILE, 'w', function (err, file) {
63-
if (err) throw err;
64-
console.log('Saved!');
65-
});
53+
fs.open(OUTPUT_FILE, 'w');
6654
const outputOptions = {
6755
input: COMBINED_FILE,
6856
output: OUTPUT_FILE

0 commit comments

Comments
 (0)