What is the output of the following code snippet? | JavaScript Fundamentals
@Yomesh Gupta
Let us consider the following code snippet, in which we are fetching a question based on the questionId
.
const User = require("../models/User");
const Question = require("../models/Question");
const { getCurrentSession } = require('../utils');
async function getQuestion(questionId) {
const isIdInvalid = !questionId || !questionId.trim().length;
if (isIdInvalid) {
throw new Error("Bad Request");
}
const question = await Question.getQuestionById(questionId);
// assume we have this utility to find current session
// computes to an object with proper user data
const session = getCurrentSession();
if (session) {
// computes to false
const isAuthorRequest = question.author.id === session.user.id;
const author = isAuthorRequest ? session.user : await User.getUserById(question.author.id);
}
return {
...question,
author: author || null,
};
};
const question = await getQuestion('Iahbvfkg5H86a6KL8yBD');
console.log(question);
Assume that all the functions getQuestionById
, getUserById
, getCurrentSession
return proper data.
If we run this code snippet then what would be the output?
Option 1
An object with question data
Option 2
undefined
Option 3
Error
Option 4
null