日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁(yè) > 科技  > 軟件

說(shuō)一說(shuō) JavaScript 異步迭代器

來(lái)源: 責(zé)編: 時(shí)間:2024-02-29 14:43:58 183觀看
導(dǎo)讀你知道嗎?除了像Promise.finally這樣的 API 之外,ECMAScript 2018還為我們帶來(lái)了另一種處理迭代器的方式——異步迭代器。問(wèn)題假設(shè)現(xiàn)在我們正處于這樣一個(gè)情景:需要使用Node.js逐行讀取文件。Node有個(gè)API叫做readLine,它

你知道嗎?除了像Promise.finally這樣的 API 之外,ECMAScript 2018還為我們帶來(lái)了另一種處理迭代器的方式——異步迭代器。ari28資訊網(wǎng)——每日最新資訊28at.com

問(wèn)題

假設(shè)現(xiàn)在我們正處于這樣一個(gè)情景:需要使用Node.js逐行讀取文件。Node有個(gè)API叫做readLine,它是一個(gè)包裝器,可用于逐行從輸入流中讀取數(shù)據(jù),不需要分析輸入緩沖區(qū)、也不需要將文本分解為小塊。ari28資訊網(wǎng)——每日最新資訊28at.com

你可以像這樣監(jiān)聽(tīng):ari28資訊網(wǎng)——每日最新資訊28at.com

const fs = require('fs')const readline = require('readline')const reader = readline.createInterface({  input: fs.createReadStream('./file.txt'),  crlfDelay: Infinity})reader.on('line', (line) => console.log(line))

假設(shè)有這樣一個(gè)簡(jiǎn)單的文件:ari28資訊網(wǎng)——每日最新資訊28at.com

line 1line 2line 3

如果我們?cè)趧?chuàng)建的文件上運(yùn)行代碼,那么就能在控制臺(tái)上逐行輸出。但是,使用事件并不是編寫(xiě)可維護(hù)代碼的最佳方法之一,因?yàn)槭录峭耆惒降?,可能?huì)中斷代碼流——因?yàn)槭菬o(wú)序觸發(fā)的,并且只能通過(guò)偵聽(tīng)器分配操作。ari28資訊網(wǎng)——每日最新資訊28at.com

解決方案

除了事件 API之外,readline還有async iterator?,F(xiàn)在我們可以不通過(guò)line事件中的偵聽(tīng)器讀取,而是通過(guò)for關(guān)鍵字來(lái)讀取。ari28資訊網(wǎng)——每日最新資訊28at.com

舉幾個(gè)使用for循環(huán)的例子。第一個(gè)是最常見(jiàn)的使用計(jì)數(shù)器和條件:ari28資訊網(wǎng)——每日最新資訊28at.com

for (let x = 0; x < array.length; x++) {  // Code here}

我們也可以使用for … in表示法讀取數(shù)組索引:ari28資訊網(wǎng)——每日最新資訊28at.com

const a = [1,2,3,4,5,6]for (let index in a) {  console.log(a[index])}

在前一種情況下,console.log輸出從1到6的數(shù)字,但是如果我們使用console.log (index),那么記錄的是數(shù)組的索引,從0到5的數(shù)字。ari28資訊網(wǎng)——每日最新資訊28at.com

下面,我們使用for … of表示法,直接獲取數(shù)組的可枚舉屬性,即它的直接值:ari28資訊網(wǎng)——每日最新資訊28at.com

const a = [1,2,3,4,5,6]for (let item of a) {  console.log(item)}

注意,這些方法都是同步的。那么,如果我們有一系列promise,這時(shí)該如何按順序讀取呢?ari28資訊網(wǎng)——每日最新資訊28at.com

假設(shè)我們還有另一個(gè)接口,它總是返回一個(gè)Promise。為了按順序解析promise,我們需要這樣做:ari28資訊網(wǎng)——每日最新資訊28at.com

async function readLine (files) {  for (const file of files) {    const line = await readFile(file) // Imagine readFile is our cursor    console.log(line)  }}

而現(xiàn)在,多虧異步可迭代對(duì)象(如readline)的魔力,我們可以執(zhí)行以下操作:ari28資訊網(wǎng)——每日最新資訊28at.com

const fs = require('fs')const readline = require('readline')const reader = readline.createInterface({  input: fs.createReadStream('./xpto.txt'),  crlfDelay: Infinity})async function read () {  for await (const line of reader) {    console.log(line)  }}read()

注意,我們現(xiàn)在使用的是for、for await (const x of y)的新定義。ari28資訊網(wǎng)——每日最新資訊28at.com

對(duì)于await和node.js

從10.x版開(kāi)始,Node.js運(yùn)行時(shí)原生支持for await表示法。如果你使用的是8.x或9.x版本,則需要使用--harmony_async_iteration標(biāo)志啟動(dòng)Javascript文件。遺憾的是,Node.js的版本6和版本7不支持異步迭代器。ari28資訊網(wǎng)——每日最新資訊28at.com

為了理解異步迭代器的概念,首先我們需要知道迭代器的本質(zhì)。簡(jiǎn)而言之,迭代器是一個(gè)對(duì)象,公開(kāi)next()函數(shù),此函數(shù)返回另一個(gè)對(duì)象,其中{value: any, done: boolean}表示當(dāng)前迭代的值,done表示序列中是否還有其他值。ari28資訊網(wǎng)——每日最新資訊28at.com

遍歷數(shù)組中所有項(xiàng)的迭代器示例如下:ari28資訊網(wǎng)——每日最新資訊28at.com

const array = [1,2,3]let index = 0const iterator = {  next: () => {    if (index >= array.length) return { done: true }    return {      value: array[index++],      done: false    }  }}

就其本身而言,迭代器沒(méi)有實(shí)際用途,那么怎么辦呢?為此,我們需要iterable。iterable是一個(gè)對(duì)象,它有一個(gè)Symbol.iterator鍵,該鍵返回的函數(shù)返回迭代器:ari28資訊網(wǎng)——每日最新資訊28at.com

// ... Iterator code here ...const iterable = {  [Symbol.iterator]: () => iterator}

現(xiàn)在我們可以正常使用迭代器了,通過(guò)for (const x of iterable),我們可以一個(gè)一個(gè)地迭代array中的所有值。ari28資訊網(wǎng)——每日最新資訊28at.com

在后臺(tái),所有數(shù)組和對(duì)象都有Symbol.iterator,這樣就可以執(zhí)行for (let x of [1,2,3])并返回我們想要的值。ari28資訊網(wǎng)——每日最新資訊28at.com

我們可以看到,異步迭代器與迭代器完全相同,不同之處在于iterable擁有的是Symbol.asyncIterator,而不是Symbol.iterator,擁有的是解析為具有相同簽名對(duì)象的Promise,而不是返回{value, done}的對(duì)象。ari28資訊網(wǎng)——每日最新資訊28at.com

讓我們把上面的迭代器變成一個(gè)異步迭代器:ari28資訊網(wǎng)——每日最新資訊28at.com

const array = [1,2,3]let index = 0const asyncIterator = {  next: () => {  if (index >= array.length) return Promise.resolve({done: true})  return Promise.resolve({value: array[index++], done: false})  }}const asyncIterable = {  [Symbol.asyncIterator]: () => asyncIterator}

異步迭代

我們可以通過(guò)調(diào)用next()函數(shù)來(lái)手動(dòng)迭代迭代器:ari28資訊網(wǎng)——每日最新資訊28at.com

// ... Async iterator Code here ...async function manual () {  const promise = asyncIterator.next() // Promise  await p // Object { value: 1, done: false }  await asyncIterator.next() // Object { value: 2, done: false }  await asyncIterator.next() // Object { value: 3, done: false }  await asyncIterator.next() // Object { done: true }}

為了遍歷異步迭代器,我們需要使用for await,但請(qǐng)記住,關(guān)鍵字await只能在異步函數(shù)中使用,所以我們需要有類(lèi)似這樣的代碼:ari28資訊網(wǎng)——每日最新資訊28at.com

// ... Code above ...async function iterate () {  for await (const num of asyncIterable) console.log(num)}iterate() // 1, 2, 3

但是,由于Node 8.x和9.x這樣的老版本不支持異步迭代器,為了在這些版本中使用異步迭代器,我們可以簡(jiǎn)單地從對(duì)象中提取next并手動(dòng)遍歷:ari28資訊網(wǎng)——每日最新資訊28at.com

// ... Async Iterator Code here ...async function iterate () {  const {next} = asyncIterable[Symbol.asyncIterator]() // we take the next iterator function  for (let {value, done} = await next(); !done; {value, done} = await next()) {    console.log(value)  }}

注意,for await更干凈、更簡(jiǎn)潔,因?yàn)樗男袨轭?lèi)似于常規(guī)循環(huán),而且,除了更易于理解之外,還可以通過(guò)done鍵自行檢查迭代器的結(jié)束。ari28資訊網(wǎng)——每日最新資訊28at.com

處理錯(cuò)誤

如果promise在迭代器中被拒絕,會(huì)發(fā)生什么?好吧,和任何被拒絕的promise一樣,通過(guò)簡(jiǎn)單的try/catch就可以來(lái)捕獲錯(cuò)誤(因?yàn)槲覀兪褂玫氖莂wait):ari28資訊網(wǎng)——每日最新資訊28at.com

const asyncIterator = { next: () => Promise.reject('Error') }const asyncIterable = { [Symbol.asyncIterator]: () => asyncIterator async function iterate () {  try {    for await (const num of asyncIterable) {}  } catch (e) {    console.log(e.message)  }}iterate()

回退

關(guān)于異步迭代器,非常有趣的一點(diǎn)是,它們有Symbol.iterator的回退,這意味著你也可以將它與常規(guī)迭代器一起使用,例如,有這樣一個(gè)promise數(shù)組:ari28資訊網(wǎng)——每日最新資訊28at.com

const promiseArray = [  fetch('https://lsantos.dev'),  fetch('https://lsantos.me')]async function iterate () {  for await (const response of promiseArray) console.log(response.status)}iterate() // 200, 200

異步生成器

在大多數(shù)情況下,迭代器和異步迭代器可以創(chuàng)建自生成器。ari28資訊網(wǎng)——每日最新資訊28at.com

生成器是允許暫停和恢復(fù)執(zhí)行的函數(shù),因此可以操作執(zhí)行,然后通過(guò)next()函數(shù)獲取下一個(gè)值。ari28資訊網(wǎng)——每日最新資訊28at.com

異步生成器的行為類(lèi)似于異步迭代器,但你必須手動(dòng)實(shí)現(xiàn)停止機(jī)制,例如,這里我們構(gòu)建一個(gè)用于git提交的隨機(jī)消息生成器:ari28資訊網(wǎng)——每日最新資訊28at.com

async function* gitCommitMessageGenerator () {  const url = 'https://whatthecommit.com/index.txt'  while (true) {    const response = await fetch(url)    yield await response.text() // We return the value  }}

注意,在任何時(shí)候都不會(huì)返回{value, done}對(duì)象,因此循環(huán)無(wú)法知道執(zhí)行何時(shí)完成。這時(shí)我們可以實(shí)現(xiàn)這樣的函數(shù):ari28資訊網(wǎng)——每日最新資訊28at.com

// Previous Codeasync function getCommitMessages (times) {  let execution = 1  for await (const message of gitCommitMessageGenerator()) {    console.log(message)    if (execution++ >= times) break  }}getCommitMessages(5)// I'll explain this when I'm sober .. or revert it// Never before had a small typo like this one caused so much damage.// For real, this time.// Too lazy to write descriptive message// Ugh. Bad rebase.

用例

再來(lái)一個(gè)更有趣的示例,為一個(gè)真實(shí)用例構(gòu)建異步迭代器。目前,適用于Node.js的Oracle數(shù)據(jù)庫(kù)驅(qū)動(dòng)程序支持resultSet API,因此可以在數(shù)據(jù)庫(kù)上執(zhí)行查詢(xún)并返回,而數(shù)據(jù)流則可以通過(guò)getRow()方法逐個(gè)讀取。ari28資訊網(wǎng)——每日最新資訊28at.com

要?jiǎng)?chuàng)建resultSet,我們需要在數(shù)據(jù)庫(kù)中執(zhí)行查詢(xún),如下所示:ari28資訊網(wǎng)——每日最新資訊28at.com

const oracle = require('oracledb')const options = {  user: 'example',  password: 'example123',  connectString: 'string'}async function start () {  const connection = await oracle.getConnection(options)  const { resultSet } = await connection.execute('query', [], { outFormat: oracle.OBJECT, resultSet: true })  return resultSet}start().then(console.log)

resultSet有一個(gè)名為getRow()的方法,這個(gè)方法從數(shù)據(jù)庫(kù)中返回要獲取的下一行的Promise。我們可以創(chuàng)建一個(gè)逐行返回此resultSet的光標(biāo)。下面讓我們創(chuàng)建Cursor類(lèi):ari28資訊網(wǎng)——每日最新資訊28at.com

class Cursor {  constructor(resultSet) {    this.resultSet = resultSet  }  getIterable() {    return {      [Symbol.asyncIterator]: () => this._buildIterator()    }  }  _buildIterator() {    return {      next: () => this.resultSet.getRow().then((row) => ({ value: row, done: row === undefined }))    }  }}module.exports = Cursor

查看光標(biāo)是否接收到它應(yīng)該處理的resultSet,并將其存儲(chǔ)在當(dāng)前狀態(tài)。因此,我們需要更改之前的方法,以便返回光標(biāo)而不是resultSet:ari28資訊網(wǎng)——每日最新資訊28at.com

const oracle = require('oracledb')const options = {  user: 'example',  password: 'example123',  connectString: 'string'}async function getResultSet() {  const connection = await oracle.getConnection(options)  const { resultSet } = await connection.execute('query', [], { outFormat: oracle.OBJECT, resultSet: true })  return resultSet}async function start() {  const resultSet = await getResultSet()  const cursor = new Cursor(resultSet)  for await (const row of cursor.getIterable()) {    console.log(row)  }}start()

這樣,我們就可以遍歷所有返回的行,不需要單獨(dú)的Promise解析。ari28資訊網(wǎng)——每日最新資訊28at.com

結(jié)論

異步迭代器非常強(qiáng)大,尤其是在Javascript等動(dòng)態(tài)和異步語(yǔ)言中。有了它們,我們就可以將復(fù)雜的執(zhí)行變成簡(jiǎn)單的代碼,從而向用戶(hù)隱藏復(fù)雜性,增加友好的用戶(hù)體驗(yàn)。ari28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-75366-0.html說(shuō)一說(shuō) JavaScript 異步迭代器

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 2023年需求最高的八大編程語(yǔ)言

下一篇: Kafka 為什么這么快?

標(biāo)簽:
  • 熱門(mén)焦點(diǎn)
Top 主站蜘蛛池模板: 鄂托克前旗| 武胜县| 古丈县| 嘉善县| 吴江市| 江川县| 汉中市| 望奎县| 贵溪市| 大姚县| 临邑县| 冀州市| 蒙自县| 偏关县| 长沙市| 鄂托克前旗| 景宁| 鄂伦春自治旗| 五家渠市| 将乐县| 台南市| 乌兰察布市| 巧家县| 郴州市| 勐海县| 田阳县| 德保县| 桐乡市| 海伦市| 共和县| 井陉县| 阜康市| 丽江市| 祁东县| 崇文区| 平利县| 浮山县| 霍邱县| 永清县| 铅山县| 绿春县|