Javascript中的Promise<Reponse>中的Reponse代表什么?
What does Reponse represent in Promise<Reponse> in Javascript?
我正在学习 Javascript 中的 fetch 方法,当我说
fetch.("url").then(r => {if(r.status!==200)doSomething();} )
当我将鼠标悬停在“获取”上时,它说它 returns Promise<Response>
,并且“then”中函数中的 r 参数是一个响应。
我的问题是:<>中的类型是什么意思?我知道在 C++ 中它代表一个模板,在本例中是 class,但是该函数如何知道“r”参数是一个 Response?
如 Mark C. 所述,获取方法 return 是类型 Response 的 Promise。
当您将鼠标悬停在 IDE 中的方法上时,您的 IDE 在内部使用 TypeScript 来推断 return 类型的类型和此方法的参数。
你的IDE帮你推断出fetch
函数的响应类型是Promise<Response>
,而<>
是TypeScript的泛型传递方式。 Response
的类型请参考https://developer.mozilla.org/en-US/docs/Web/API/Response
其实这只是IDE辅助写作JavaScript的一种方式。如果你想要严格的类型检查,请使用TypeScript。
What does Reponse represent in Promise in Javascript?
没有。 Promise<Response>
是无效的 Java 脚本语法,因此它没有任何意义。
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?
javascript 编程语言本身不附带编辑器或 IDE。大多数人在他们最喜欢的文本编辑器中编写 javascript。您将需要查看代码编辑器的文档或 IDE 以了解弹出窗口的含义。
不过我们可以猜到它的意思。 <>
是无效的 javascript 语法,因此开发代码编辑器的程序员可能从 C++/Java 中获得灵感。我的猜测是它在 C++/Java 中意味着同样的事情——因为 Promise 是一个 template/generic (它实际上不是 javascript 因为没有这样的东西)从 C++/Java 程序员的角度来看,它将编译为一个对象,该对象的 .then()
方法需要一个参数为 <>
类型的函数(类似于 C++ 中的 Futures和 Java).
How does the function know that the "r" parameter is a Response?
没有。编写 Promise 接口的程序员希望您将接受一个参数的函数传递给它的 .then()
方法。然后程序员的代码将 调用 您传递给 .then()
的函数并将其传递给 Response
对象。
这是一个例子。让我写这个简单的函数:
function sayHello (formatter) {
console.log(formatter("Hello World"));
}
现在让我们使用上面的函数:
sayHello(h => {
return "This is a " + h.toUpperCase() + " program";
})
// output = This is a HELLO WORLD program
你的问题是 sayHello
函数如何知道 h
参数是一个值为“Hello World”的字符串?
答案很简单,因为我这个写sayHello()
函数的程序员传给你
你 怎么知道 h
是字符串“Hello World”?好吧,希望我会在某个地方写一些文档供您阅读。
那么您如何知道代码中 r
的值是多少?您需要阅读文档:https://developer.mozilla.org/en-US/docs/Web/API/fetch。具体第二段第一句:
The promise resolves to the Response object representing the response to your request.
我正在学习 Javascript 中的 fetch 方法,当我说
fetch.("url").then(r => {if(r.status!==200)doSomething();} )
当我将鼠标悬停在“获取”上时,它说它 returns Promise<Response>
,并且“then”中函数中的 r 参数是一个响应。
我的问题是:<>中的类型是什么意思?我知道在 C++ 中它代表一个模板,在本例中是 class,但是该函数如何知道“r”参数是一个 Response?
如 Mark C. 所述,获取方法 return 是类型 Response 的 Promise。
当您将鼠标悬停在 IDE 中的方法上时,您的 IDE 在内部使用 TypeScript 来推断 return 类型的类型和此方法的参数。
你的IDE帮你推断出fetch
函数的响应类型是Promise<Response>
,而<>
是TypeScript的泛型传递方式。 Response
的类型请参考https://developer.mozilla.org/en-US/docs/Web/API/Response
其实这只是IDE辅助写作JavaScript的一种方式。如果你想要严格的类型检查,请使用TypeScript。
What does Reponse represent in Promise in Javascript?
没有。 Promise<Response>
是无效的 Java 脚本语法,因此它没有任何意义。
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?
javascript 编程语言本身不附带编辑器或 IDE。大多数人在他们最喜欢的文本编辑器中编写 javascript。您将需要查看代码编辑器的文档或 IDE 以了解弹出窗口的含义。
不过我们可以猜到它的意思。 <>
是无效的 javascript 语法,因此开发代码编辑器的程序员可能从 C++/Java 中获得灵感。我的猜测是它在 C++/Java 中意味着同样的事情——因为 Promise 是一个 template/generic (它实际上不是 javascript 因为没有这样的东西)从 C++/Java 程序员的角度来看,它将编译为一个对象,该对象的 .then()
方法需要一个参数为 <>
类型的函数(类似于 C++ 中的 Futures和 Java).
How does the function know that the "r" parameter is a Response?
没有。编写 Promise 接口的程序员希望您将接受一个参数的函数传递给它的 .then()
方法。然后程序员的代码将 调用 您传递给 .then()
的函数并将其传递给 Response
对象。
这是一个例子。让我写这个简单的函数:
function sayHello (formatter) {
console.log(formatter("Hello World"));
}
现在让我们使用上面的函数:
sayHello(h => {
return "This is a " + h.toUpperCase() + " program";
})
// output = This is a HELLO WORLD program
你的问题是 sayHello
函数如何知道 h
参数是一个值为“Hello World”的字符串?
答案很简单,因为我这个写sayHello()
函数的程序员传给你
你 怎么知道 h
是字符串“Hello World”?好吧,希望我会在某个地方写一些文档供您阅读。
那么您如何知道代码中 r
的值是多少?您需要阅读文档:https://developer.mozilla.org/en-US/docs/Web/API/fetch。具体第二段第一句:
The promise resolves to the Response object representing the response to your request.