简体   繁体   English

为什么这个箭头 function 返回一个 Promise{<pending> }</pending>

[英]Why does this arrow function return a Promise{<pending>}

I'm working on reading a JSON file into my node.js code however in my retrieveTeamData function, I am getting a Promise{} in my console output. I'm working on reading a JSON file into my node.js code however in my retrieveTeamData function, I am getting a Promise{} in my console output. Originally, I have not used the async/await inside the retrieveTeamData function but I thought it would help in case the issue was that the promise times out before it resolves.最初,我没有在retrieveTeamData function 中使用异步/等待,但我认为如果问题是promise 在解决之前超时会有所帮助。

    const PLAYERS_PATH = path.join(__dirname, 'json_files', 'absences.json');
const COACHES_PATH = path.join(__dirname, 'json_files', 'members.json');

const readJsonFile = (path) => new Promise((resolve) => fs.readFile(path, 'utf8', (_, data) => resolve(data)))
    .then((data) => JSON.parse(data))
    .then((data) => data.stats);


   async function retrieveTeamData() {
      const players = async () => await readJsonFile(PLAYERS_PATH);
      const coaches  = async() => await readJsonFile(COACHES_PATH);
    console.log(players());
    console.log(coaches());
}

Because it returns a promise.因为它返回一个 promise。 The then method of a promise returns a promise that will resolve based on what happens to the promise you called it on and what happens in the callback you provide it. promise 的then方法返回一个 promise ,它将根据您调用它的 promise 发生的情况以及您提供的回调中发生的情况来解决。 Similarly, an async function always returns a promise, which is resolved to what happens in the function.类似地, async function 总是返回 promise,这解决了 function 中发生的情况。 So players() (for instance) returns a promise.所以 player players() (例如)返回一个 promise。

You can't synchronously use the result of an asynchronous call.您不能同步使用异步调用的结果。 You have to use it asynchronously .您必须异步使用它。


Note that there's no purpose served by:请注意,以下人员没有任何目的:

const players = async () => await readJsonFile(PLAYERS_PATH);

followed by其次是

players();

Just use只需使用

readJsonFile(PLAYERS_PATH);

directly.直接地。 Eg (in an async function):例如(在async函数中):

console.log(await readJsonFile(PLAYERS_PATH));

Since that's in an async function, you can use await to wait for the value.由于这是在async function 中,您可以使用await等待该值。 Just beware that whatever is calling that async function at the outermost layer has to handle the fact that it does its work asynchronously and it may fail (so must must .then and .catch ).请注意,在最外层调用async function 的任何东西都必须处理它异步工作并且可能失败的事实(因此必须必须.then.catch )。

Result of async function is always Promise, so you can await for it. async function 的结果总是 Promise,所以你可以await它。

You aren't awaiting the output for the console.log statements, so they're firing before the promise is resolved.您无需等待 output 用于 console.log 语句,因此它们会在 promise 解决之前触发。 Try attaching your console.log statements with.then chains to the statement that calls your async function.尝试将带有.then 链的 console.log 语句附加到调用异步 function 的语句。

That is, players().then(console.log(players()));players().then(console.log(players()));

That's how you would do it, regardless of whether this is the best approach.无论这是否是最好的方法,你都会这样做。

It's because you're making new async methods for reach readJsonFile and not waiting for them to resolve before logging.这是因为您正在为到达readJsonFile创建新的异步方法,而不是在记录之前等待它们解决。 I think what you might want is:我想你可能想要的是:

const players = await readJsonFile(PLAYERS_PATH);
const coaches = await readJsonFile(COACHES_PATH);

If you wanted to keep your original syntax, you would then need to use await before logging them.如果您想保留原始语法,则需要在记录它们之前使用await

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

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