错误:仅当异步功能中已有功能时,等待才在异步功能中有效 [英] Error: await is only valid in async function when function is already within an async function

查看:59
本文介绍了错误:仅当异步功能中已有功能时,等待才在异步功能中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:从我的目录中获取文件列表;为每个文件获取SHA256

错误: await仅在异步函数中有效

我不确定为什么会这样,因为我的函数已经包装在异步函数中了.任何帮助,我们将不胜感激!

  const hasha = require('hasha');const getFiles =()=>{fs.readdir('PATH_TO_FILE',(err,files)=> {files.forEach(i => {返回我});});}(异步()=> {const getAllFiles = getFiles()getAllFiles.forEach(i => {const hash = await hasha.fromFile(i,{algorithm:'sha256'});返回console.log(hash);})}); 

解决方案

您的 await 不在 async 函数内,因为它位于 .forEach()回调,但未声明为 async .

您真的需要重新考虑如何处理此问题,因为 getFiles()甚至什么都没有返回.请记住,从回调返回只是从该回调返回,而不是从父函数返回.

这是我的建议:

  const fsp = require('fs').promises;const hasha = require('hasha');异步函数getAllFiles(){让文件=等待fsp.readdir('PATH_TO_FILE');用于(让文件中的文件){const hash = await hasha.fromFile(i,{algorithm:'sha256'});console.log(哈希);}}getAllFiles().then(()=> {console.log(全部完成");}).catch(err => {console.log(err);}); 

在这个新的实现中:

  1. 使用 const fsp = require('fs').promises 获取 fs 模块的promises接口.
  2. 使用 await fsp.readdir()来使用Promise读取文件
  3. 使用 for/of 循环,以便我们可以使用 await 正确地对异步操作进行排序.
  4. 调用该函数并监视完成和错误.

Goal: Get a list of files from my directory; get the SHA256 for each of those files

Error: await is only valid in async function

I'm not sure why that is the case since my function is already wrapped inside an async function.. any help is appreciated!

const hasha = require('hasha');

const getFiles = () => {
    fs.readdir('PATH_TO_FILE', (err, files) => {
        files.forEach(i => {
           return i;
        });
    });   
}
(async () => {
    const getAllFiles = getFiles()
    getAllFiles.forEach( i => {
        const hash = await hasha.fromFile(i, {algorithm: 'sha256'});
        return console.log(hash);
    })
});

解决方案

Your await isn't inside an async function because it's inside the .forEach() callback which is not declared async.

You really need to rethink how you approach this because getFiles() isn't even returning anything. Keep in mind that returning from a callback just returns from that callback, not from the parent function.

Here's what I would suggest:

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

async function getAllFiles() {
    let files = await fsp.readdir('PATH_TO_FILE');
    for (let file of files) {
        const hash = await hasha.fromFile(i, {algorithm: 'sha256'});
        console.log(hash);            
    }
}

getAllFiles().then(() => {
    console.log("all done");
}).catch(err => {
    console.log(err);
});

In this new implementation:

  1. Use const fsp = require('fs').promises to get the promises interface for the fs module.
  2. Use await fsp.readdir() to read the files using promises
  3. Use a for/of loop so we can properly sequence our asynchronous operations with await.
  4. Call the function and monitor both completion and error.

这篇关于错误:仅当异步功能中已有功能时,等待才在异步功能中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆