3

Смотрю туториал и автор не объяснил, зачем делает так:

let request = new Request(url);
fetch(request);

Зачем он создал экземпляр от Request? Если можно было через XMLHTTP, или просто бы он мог пихнуть url в fetch. Я знаю, что вы без полного кода не поймёте. Вот ссылка: https://jsfiddle.net/v05e76zt/ Объясните разницу пожалуйста между new Request, new XMLHTTPRequest. Мне кажется второй легче. И зачем пихать request в fetch. Туда ведь вроде url достаточно пихнуть.

2

1 ответ 1

3

Постараюсь ответить покороче.

Давайте разберемся для начала. Зачем нужен fetch если есть XMLHttpRequest?

  1. fetch API современный. А точнее

Fetch is a new native JavaScript API, supported by most browsers today. Fetch allows you to make network requests similar to XMLHttpRequest. According to Google Developers Documentation Fetch makes it easier to make asynchronous requests and handle responses better than with the older XMLHttpRequest. It is an improvement over the XMLHttpRequest API. The main difference between Fetch and XMLHttpRequest is that the Fetch API uses Promises, hence avoiding callback hell.

Все почти тоже самое, а главное fetch api это про промисы. А промисы это решение против коллбек хела.

  1. Так зачем Request? А он часть fetch API.

The Request interface of the Fetch API represents a resource request.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.