繁体   English   中英

如何将读取数据从 await readFile 传递到 node.js 的 fs 模块中的 writeFile?

[英]How to pass read data from await readFile to writeFile in fs module of node.js?

在此代码中,文件正在打开并成功读取。

var objFs = require('fs')

async function openFile() {
    await objFs.open('new.js', 'r', (argError, argFD) => {
        if (argError)
            throw -1
        else
            console.log("File opened!")
    })

    // This object is the promise.                  
    const objReadResult = await objFs.readFile('new.js', (argError, argData) => {
        if (argError)
            throw 2
        else
            console.log('File read!')
    })

    await objFs.writeFile('new.js', (argError, objReadResult) => {
                                  try
                                    {
                                        if( argError )
                                            throw argError
                                        else
                                            console.log("File written")
                                    }
                                    catch( arg )
                                    {
                                        console.log(arg)
                                    }        
    })


}

openFile()

从 readFile 的 await 中提取数据并将其传递给 writeFile 的方法是什么?

我编写 writeFile 函数的方式产生了以下错误:

(node:21737) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
    at maybeCallback (fs.js:145:9)
    at Object.writeFile (fs.js:1332:14)
    at openFile (/home/sulakshana/Documents/nodejs/programs/async_await.js:29:14)
(node:21737) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:21737) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
File opened!
File read!
 

我能看到的几个问题:

  • 有多种语法 - async/await同时还提供对fs调用的回调;
  • 无需在fs.open之前调用fs.readFile
  • fs.writeFile [ docs ] 中没有要写入的数据;
  • 最后应该有await -ing;
  • 最后(报告的实际错误) - 应该有try...catch块来捕获并响应发生的任何错误。

示例实现:

const fs = require('fs').promises;

async function openFile() {
    const objReadResult = await fs.readFile('new.js');
    console.log('File read!');
    await fs.writeFile('new.js', /* PROVIDE DATA TO WRITE */);
    console.log('File written');
}

(async () => {
    try {
        await openFile();
        console.log('Program is done');
    } catch (err) {
        console.error(err);
    }
})();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM