shows error message when request req fails

This commit is contained in:
ajaythapliyal
2023-02-22 10:02:05 +05:30
parent 41742dccd3
commit 1fc9d61325
6 changed files with 41 additions and 6 deletions

View File

@@ -5,7 +5,7 @@ export function fetchAnswerApi(
apiKey: string,
): Promise<Answer> {
// a mock answer generator, this is going to be replaced with real http call
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
let result = '';
const characters =
@@ -18,7 +18,16 @@ export function fetchAnswerApi(
);
counter += 1;
}
resolve({ answer: result, query: question, result });
const randNum = getRandomInt(0, 10);
randNum < 5
? reject()
: resolve({ answer: result, query: question, result });
}, 3000);
});
}
function getRandomInt(min: number, max: number) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
}