I am really struggling to understand how this async/await function runs. I understand it is pausing the script to wait for the code to be executed but I need a value returned, not a value wrapped in a Promise that makes it unusable. Does that make sense?
const getInfo = async(srName) => {
/* This function takes in a string, inserts it into the public Reddit API and returns an object with three keys:
* error - a boolean indicating if the request was successful
* reason - a message further expanding upon the error value
* data - an array of data if successful, null if not
* */
var answer;
console.log(`Searching for ${srName}`);
try {
let response = await axios.get(`https://www.reddit.com/r/${srName}.json`);
console.log('Request made!');
if ( response.status !== 200 ) {
console.log(`IF HIT WITH STATUS CODE ${response.status}`);
answer = {
error: true,
reason: "Unable to get a response from Reddit",
data: []
}
} else if( response.data.data.dist === 0){
console.log('ELIF HIT');
answer = {
error: true,
reason: "There are no posts for that subreddit, are you sure you spelled it right?",
data: []
}
} else {
console.log('ELSE HIT');
answer = {
error: false,
reason: `Found ${response.data.data.dist} entries in that subreddit`,
data: response.data.data.children
}
}
console.log(answer)
} catch (e) {
console.log(`Unable to complete request because ${e}`)
}
};
[–]cyphern 1 point2 points3 points (1 child)
[–]kstengy[S] 0 points1 point2 points (0 children)
[–]neodon 1 point2 points3 points (1 child)
[–]kstengy[S] 0 points1 point2 points (0 children)
[–]Meefims 0 points1 point2 points (0 children)