第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

async for 循環(huán)導致函數(shù)靜默終止

async for 循環(huán)導致函數(shù)靜默終止

慕哥6287543 2023-08-10 14:39:40
您好,我試圖了解以下代碼中發(fā)生的情況:import fs from "fs";import util from "util";import split2 from "split2";async function main() {    const files = await fs.promises.readdir("..");    for (const f of files) {        const s = fs.createReadStream("../" + f);        const ss = s.pipe(split2());        s.on("error", (err) => {            console.log(err);        });        for await (const l of ss) {        }        console.log(f);    }    console.log("Returning");    return "Done";}main();它基本上讀取目錄中的每個文件并迭代每個文件的行(通過使用 forawait 語法將可讀流通過管道輸送到 split2 庫中。問題是,當文件確實是目錄時,createReadStream 失?。S后什么也沒有發(fā)生)奇怪的是,似乎一個錯誤導致主函數(shù)默默退出并在錯誤之前僅顯示幾個文件名。.gitignoreapp.js[Error: EISDIR: illegal operation on a directory, read] {  errno: -21,  code: 'EISDIR',  syscall: 'read'}如果我只是評論await for 循環(huán),我會得到以下輸出。.gitignoreapp.jsbusinesscomponentsconfigenv.skeletronnode_modulespackage-lock.jsonReturning[Error: EISDIR: illegal operation on a directory, read] {  errno: -21,  code: 'EISDIR',  syscall: 'read'}[Error: EISDIR: illegal operation on a directory, read] {  errno: -21,  code: 'EISDIR',  syscall: 'read'}[Error: EISDIR: illegal operation on a directory, read] {  errno: -21,  code: 'EISDIR',  syscall: 'read'}[Error: EISDIR: illegal operation on a directory, read] {  errno: -21,  code: 'EISDIR',  syscall: 'read'}[Error: EISDIR: illegal operation on a directory, read] {  errno: -21,  code: 'EISDIR',  syscall: 'read'}[Error: EISDIR: illegal operation on a directory, read] {  errno: -21,  code: 'EISDIR',  syscall: 'read'}[Error: EISDIR: illegal operation on a directory, read] {  errno: -21,  code: 'EISDIR',  syscall: 'read'}[Error: EISDIR: illegal operation on a directory, read] {  errno: -21,  code: 'EISDIR',  syscall: 'read'}  這確實是完整的文件列表,并且各個錯誤不會停止外循環(huán)。我在 Ubuntu 機器上使用節(jié)點 v14.0.0 運行它。知道發(fā)生了什么事嗎?我真的迷路了:-D
查看完整描述

1 回答

?
Helenr

TA貢獻1780條經(jīng)驗 獲得超4個贊

您沒有處理循環(huán)中的錯誤for await (const l of ss) {...}。當您嘗試在目錄上運行它時,您的for await循環(huán)將拒絕,并且由于您沒有捕獲或處理該拒絕,因此包含的async函數(shù)也將拒絕并且程序?qū)⑼V埂?/p>

您可以嘗試在循環(huán)中放置 try/catchfor await并查看是否有效。但是,我在流功能中發(fā)現(xiàn)了許多錯誤for await,甚至提交了一些錯誤。如果打開文件時出錯或讀取文件時出錯,則會出現(xiàn)錯誤,因此此功能只是有錯誤,因此我決定不在我的代碼中使用它。

另請注意,您沒有一個好的方法來從事件中傳達錯誤,error因為您無法拒絕async嵌套事件處理程序內(nèi)部的函數(shù)。總體而言,流及其事件與基于承諾的編程不能很好地融合。有多種方法可以承諾某些流事件,但要使流與承諾良好配合,還需要進行中。


查看完整回答
反對 回復 2023-08-10
  • 1 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號