Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
What does Reponse represent in Promise<Reponse> in Javascript?

I'm learning about the fetch method in Javascript and I when I say

fetch.("url").then(r => {if(r.status!==200)doSomething();} )

and when I hover over "fetch" it says that it returns Promise<Response>, and the r parameter in the function in "then" is a Response.

My question is: what does the type in <> mean? I know that in C++ it represents a template, in this case a class, but how does the function know that the "r" parameter is a Response?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Your IDE helped you infer that the response type of the fetch function is Promise<Response>, and <> is the way of passing generic types of TypeScript. For the type of Response, please refer to https: //developer.mozilla.org/en-US/docs/Web/API/Response

In fact this is just a way for the IDE to assist in writing JavaScript. If you want strict type checking, please use TypeScript.

about 4 years ago · Juan Pablo Isaza Relatório

0

What does Reponse represent in Promise in Javascript?

Nothing. Promise<Response> is not valid Javascript syntax so it does not mean anything.

When I hover over "fetch" it says that it returns Promise, and the r parameter in the function in "then" is a Response... My question is: what does the type in <> mean?

The javascript programming language itself does not come with an editor or IDE. Most people write javascript in their favorite text editor. You will need to look at the documentation of your code editor or IDE to know what the popup means.

However we can guess what it means. <> is not valid javascript syntax so the programmer who developed the code editor probably took inspiration from C++/Java. My guess is it means the same thing in C++/Java - that being that the Promise is a template/generic (it is not really in javascript since there is no such thing) if you look at it from a C++/Java programmer's point of view and it will compile to an object who's .then() method expects a function who's argument is of the type in <> (similar to Futures in C++ and Java).

How does the function know that the "r" parameter is a Response?

It does not. The programmer who wrote the Promise interface expect you to pass a function who accepts one argument to its .then() method. The programmer's code will then call the function you pass to .then() and pass it the Response object.

Here's an example. Let me write this simple function:

function sayHello (formatter) {
    console.log(formatter("Hello World"));
}

Now let's use the function above:

sayHello(h => {
    return "This is a " + h.toUpperCase() + " program";
})
// output = This is a HELLO WORLD program

Your question is how does the sayHello function know that the h parameter is a string who's value is "Hello World"?

The answer is simple, It's because I, the programmer who wrote the sayHello() function, pass it to you.

How would you know that h is the string "Hello World"? Well, hopefully I would have written some documentation somewhere for you to read.

So how would you know what is the value of r in your code? You need to read the documentation: https://developer.mozilla.org/en-US/docs/Web/API/fetch. Specifically the first sentence in the second paragraph:

The promise resolves to the Response object representing the response to your request.

about 4 years ago · Juan Pablo Isaza Relatório

0

As mentioned by Mark C., The fetch method returns a Promise of type Response.

When you hover over the method in your IDE, your IDE uses TypeScript internally to infer the types of the return type and arguments of this method.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda