/
austin_s
/
react-js-interview-task-jr
Обзор
Документация
Войти
/
austin_s
/
react-js-interview-task-jr
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/utils/request.ts
41 строка
1 KB
spanic
Adds promisification task
27 апр 2026, 06:36
27 апр 2026, 06:36
58e2cf2
Код
Авторство
О чём код?
/** * Callback-based GET request using XMLHttpRequest. * * @param url - The URL to fetch * @param onSuccess - Called with parsed JSON response on success * @param onError - Called with an Error on failure */ export function get<T>( url: string, onSuccess: (data: T) => void, onError: (error: Error) => void ): void { const xhr = new XMLHttpRequest() xhr.addEventListener('load', () => { if (xhr.status >= 200 && xhr.status < 300) { onSuccess(JSON.parse(xhr.responseText) as T) } else { onError(new Error(`HTTP ${xhr.status}: ${xhr.statusText}`)) } }) xhr.addEventListener('error', () => { onError(new Error('Network error')) }) xhr.open('GET', url) xhr.send() } /** * TODO: Implement a promisified version of the `get` function above. * * Requirements: * - Must return a Promise<T> that resolves with parsed JSON on success * - Must reject with an Error on failure (HTTP errors and network errors) * - Must use the callback-based `get` function internally */ export function getAsync<T>(url: string): Promise<T> { throw new Error('Not implemented') }