- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
Fetch 是用于发出网络请求的新的基于 Promise 的 API:
fetch('https://www.everythingisawesome.com/')
.then(response => console.log('status: ', response.status));
这对我来说很有意义 - 当我们发起网络调用时,我们返回一个 Promise,让我们的线程继续处理其他业务。当响应可用时,Promise 中的代码就会执行。
但是,如果我对响应的有效负载感兴趣,我会通过响应的方法而不是属性来实现:
这些方法返回 promise ,我不清楚为什么。
fetch('https://www.everythingisawesome.com/') //IO bound
.then(response => response.json()); //We now have the response, so this operation is CPU bound - isn't it?
.then(entity => console.log(entity.name));
为什么处理响应的有效负载会返回一个 promise - 我不清楚为什么它应该是一个异步操作。
最佳答案
Why are these fetch methods asynchronous?
天真的答案是 "because the specification says so"
- The
arrayBuffer()
method, when invoked, must return the result of running consume body with ArrayBuffer.- The
blob()
method, when invoked, must return the result of running consume body with Blob.- The
formData()
method, when invoked, must return the result of running consume body with FormData.- The
json()
method, when invoked, must return the result of running consume body with JSON.- The
text()
method, when invoked, must return the result of running consume body with text.
当然,这并没有真正回答问题,因为它留下了“规范为什么这么说?”的问题。
这就是事情变得复杂的地方,因为我确定推理,但我没有来自官方来源的证据来证明这一点。我将尝试尽我所能解释理性,但请注意,此处之后的所有内容都应主要视为外部意见。
当您使用获取 API 从资源请求数据时,您必须等待资源完成下载才能使用它。这应该是相当明显的。 JavaScript 使用异步 API 来处理此行为,因此所涉及的工作不会阻塞其他脚本,更重要的是,不会阻塞 UI。
资源下载完成后,数据量可能会很大。没有什么可以阻止您请求超过 50MB 的整体 JSON 对象。
如果您尝试同步解析 50MB 的 JSON,您认为会发生什么?它会阻止其他脚本,更重要的是会阻止 UI。
其他程序员已经解决了如何以高性能方式处理大量数据的问题:Streams。在 JavaScript 中,流是使用异步 API 实现的,因此它们不会阻塞,如果您阅读 consume body细节,很明显流被用来解析数据:
Let stream be body's stream if body is non-null, or an empty ReadableStream object otherwise.
现在,该规范当然有可能定义两种访问数据的方式:一种用于较小数据量的同步 API,一种用于较大量数据的异步 API,但这会导致混淆和重复。
此外Ya Ain't Gonna Need It .任何可以用同步代码表达的东西都可以用异步代码表达。反之则不然。因此,创建了一个可以处理所有用例的异步 API。
关于javascript - 为什么这些获取方法是异步的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39170556/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!