Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用async解决此错误?_Javascript_Node.js_Asynchronous_Try Catch - Fatal编程技术网

Javascript 如何使用async解决此错误?

Javascript 如何使用async解决此错误?,javascript,node.js,asynchronous,try-catch,Javascript,Node.js,Asynchronous,Try Catch,我从node.js开始,尝试使用try..catch和文件系统方法。这就是我所拥有的: let fs = require("fs"); const util = require('util'); let buf = new Buffer(1024); const open = util.promisify(fs.open); const read = util.promisify(fs.read); const close = util.promisify(fs.close) async

我从node.js开始,尝试使用try..catch和文件系统方法。这就是我所拥有的:

let fs = require("fs");
const util = require('util');

let buf = new Buffer(1024);

const open = util.promisify(fs.open);
const read = util.promisify(fs.read);
const close = util.promisify(fs.close)

async function main(){
  console.log("Going to open an existing file");
  try{
    const fd = await open('input.txt', 'r+');
    console.log("File opened successfully!");
    console.log("Going to read the file");

    try{
      await read(fd, buf, 0, buf.length, 0);
      if(bytes > 0) 
         console.log(buf.slice(0, bytes).toString());
    }
    catch(e){
      console.log("Error");
   } 
 }
 catch(e){
   console.log("Error");
 }
} 
main();
console.log("Program ended");
当我执行时,它进入第二个catch,不打印任何内容


对于更新的代码,我看到的唯一错误来自未声明的
字节
变量

等待读取(fd,buf,0,buf.length,0)将返回一个值,该值具有:

{ 
  buffer: bufferData,
  bytesRead: numberOfBytes
} 
因此,在异步代码中,您需要获得并使用它们:

try{
  let ret = await read(fd, buf, 0, buf.length, 0);
  if(ret.bytesRead > 0) 
     console.log(buf.slice(0, ret.bytesRead).toString());
}
当然,您也可以只打印从
read()
返回的缓冲区

catch
块中打印错误也很有帮助:

catch(e){
  console.log("Error", e);
} 
此外,如果希望结尾处的
console.log
处于正确的时间,则应使用以下命令:

main().then(() => console.log("Program ended")) ;

对于更新的代码,我看到的唯一错误来自未声明的
字节
变量

等待读取(fd,buf,0,buf.length,0)将返回一个值,该值具有:

{ 
  buffer: bufferData,
  bytesRead: numberOfBytes
} 
因此,在异步代码中,您需要获得并使用它们:

try{
  let ret = await read(fd, buf, 0, buf.length, 0);
  if(ret.bytesRead > 0) 
     console.log(buf.slice(0, ret.bytesRead).toString());
}
当然,您也可以只打印从
read()
返回的缓冲区

catch
块中打印错误也很有帮助:

catch(e){
  console.log("Error", e);
} 
此外,如果希望结尾处的
console.log
处于正确的时间,则应使用以下命令:

main().then(() => console.log("Program ended")) ;

可能重复为什么要将所有这些参数传递给
read()
,而不是
buffer=wait read(fd)?不想要路径或缓冲区吗?读取不=readFile@Keith参见->
const read=util.promisify(fs.readFile)
@JasonMort,我理解这一点,但是
read
fs.readFile
的预期版本,所以您需要像
readFile
一样将参数传递到
read
。你要传递5个参数,为什么?您可以这样使用
read()
let buffer=wait read(fd)
为什么要将所有这些参数传递给
read()
而不是
buffer=await read(fd)的可能重复项?不想要路径或缓冲区吗?读取不=readFile@Keith参见->
const read=util.promisify(fs.readFile)
@JasonMort,我理解这一点,但是
read
fs.readFile
的预期版本,所以您需要像
readFile
一样将参数传递到
read
。你要传递5个参数,为什么?您可以这样使用
read()
let buffer=wait read(fd)