Skip to content

Commit 42c9587

Browse files
committed
Refactor examples.js to use async/await
1 parent 6edd547 commit 42c9587

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

server/scripts/examples.js

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ mongoose.connection.on('error', () => {
4646
process.exit(1);
4747
});
4848

49-
function getCategories() {
49+
async function getCategories() {
5050
const categories = [];
5151
const options = {
5252
url: 'https://api.github.com/repos/processing/p5.js-website/contents/src/data/examples/en',
@@ -57,23 +57,23 @@ function getCategories() {
5757
},
5858
json: true
5959
};
60-
return rp(options).then((res) => {
60+
try {
61+
const res = await rp(options);
6162
res.forEach((metadata) => {
6263
let category = '';
6364
for (let j = 1; j < metadata.name.split('_').length; j += 1) {
6465
category += `${metadata.name.split('_')[j]} `;
6566
}
6667
categories.push({ url: metadata.url, name: category.trim() });
6768
});
68-
6969
return categories;
70-
}).catch((err) => {
71-
throw err;
72-
});
70+
} catch (error) {
71+
throw error;
72+
}
7373
}
7474

7575
function getSketchesInCategories(categories) {
76-
return Q.all(categories.map((category) => {
76+
return Q.all(categories.map(async (category) => {
7777
const options = {
7878
url: `${category.url.replace('?ref=main', '')}`,
7979
method: 'GET',
@@ -83,8 +83,8 @@ function getSketchesInCategories(categories) {
8383
},
8484
json: true
8585
};
86-
87-
return rp(options).then((res) => {
86+
try {
87+
const res = await rp(options);
8888
const projectsInOneCategory = [];
8989
res.forEach((example) => {
9090
let projectName;
@@ -103,14 +103,14 @@ function getSketchesInCategories(categories) {
103103
}
104104
});
105105
return projectsInOneCategory;
106-
}).catch((err) => {
107-
throw err;
108-
});
106+
} catch (error) {
107+
throw error;
108+
}
109109
}));
110110
}
111111

112112
function getSketchContent(projectsInAllCategories) {
113-
return Q.all(projectsInAllCategories.map(projectsInOneCategory => Q.all(projectsInOneCategory.map((project) => {
113+
return Q.all(projectsInAllCategories.map(projectsInOneCategory => Q.all(projectsInOneCategory.map(async (project) => {
114114
const options = {
115115
url: `${project.sketchUrl.replace('?ref=main', '')}`,
116116
method: 'GET',
@@ -119,8 +119,8 @@ function getSketchContent(projectsInAllCategories) {
119119
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`
120120
}
121121
};
122-
123-
return rp(options).then((res) => {
122+
try {
123+
const res = await rp(options);
124124
const noNumberprojectName = project.projectName.replace(/(\d+)/g, '');
125125
if (noNumberprojectName === 'Instance Mode: Instance Container ') {
126126
for (let i = 0; i < 4; i += 1) {
@@ -134,9 +134,9 @@ function getSketchContent(projectsInAllCategories) {
134134
project.sketchContent = res;
135135
}
136136
return project;
137-
}).catch((err) => {
138-
throw err;
139-
});
137+
} catch (error) {
138+
throw error;
139+
}
140140
}))));
141141
}
142142

@@ -183,14 +183,13 @@ async function addAssetsToProject(assets, response, project) {
183183
};
184184

185185
// a function to await for the response that contains the content of asset file
186-
const doRequest = function (optionsAsset) {
187-
return new Promise(((resolve, reject) => {
188-
rp(optionsAsset).then((res) => {
189-
resolve(res);
190-
}).catch((err) => {
191-
reject(err);
192-
});
193-
}));
186+
const doRequest = async (optionsAsset) => {
187+
try {
188+
const res = await rp(optionsAsset);
189+
return res;
190+
} catch (error) {
191+
throw error;
192+
}
194193
};
195194

196195
assetContent = await doRequest(assetOptions);
@@ -225,7 +224,7 @@ async function addAssetsToProject(assets, response, project) {
225224
}
226225

227226

228-
function createProjectsInP5user(projectsInAllCategories) {
227+
async function createProjectsInP5user(projectsInAllCategories) {
229228
const options = {
230229
url: 'https://api.github.com/repos/processing/p5.js-website/contents/src/data/examples/assets',
231230
method: 'GET',
@@ -236,7 +235,8 @@ function createProjectsInP5user(projectsInAllCategories) {
236235
json: true
237236
};
238237

239-
rp(options).then((res) => {
238+
try {
239+
const res = await rp(options);
240240
User.findOne({ username: 'p5' }, (err, user) => {
241241
if (err) throw err;
242242

@@ -347,14 +347,14 @@ function createProjectsInP5user(projectsInAllCategories) {
347347
process.exit();
348348
});
349349
});
350-
}).catch((err) => {
351-
throw err;
352-
});
350+
} catch (error) {
351+
throw error;
352+
}
353353
}
354354

355355
function getp5User() {
356356
console.log('Getting p5 user');
357-
User.findOne({ username: 'p5' }, (err, user) => {
357+
User.findOne({ username: 'p5' }, async (err, user) => {
358358
if (err) throw err;
359359

360360
let p5User = user;
@@ -380,10 +380,11 @@ function getp5User() {
380380
});
381381
});
382382

383-
return getCategories()
384-
.then(getSketchesInCategories)
385-
.then(getSketchContent)
386-
.then(createProjectsInP5user);
383+
const categories = await getCategories();
384+
const sketchesInCategories = await getSketchesInCategories(categories);
385+
const sketchContent = await getSketchContent(sketchesInCategories);
386+
const projects = createProjectsInP5user(sketchContent);
387+
return projects;
387388
});
388389
}
389390

0 commit comments

Comments
 (0)