简体   繁体   English

如何获取异步的值并传递给 function?

[英]How can I get the value of a async and pass to a function?

I need to get the value of mtime and return as result of lastSave function but i get undefined.我需要获取 mtime 的值并作为 lastSave function 的结果返回,但我没有定义。 I know I can use fs.statSync("./Data.xlsx") to get this value.我知道我可以使用 fs.statSync("./Data.xlsx") 来获取这个值。 I'm using fs.stat() as example for async inside a function.我使用 fs.stat() 作为 function 中的异步示例。

const lastSave = async (file) => {
    const momento = await fs.stat(file, function (err, stats) {
        var mtime = stats.mtime;
        console.log("interno", mtime);    // this works
        return mtime
    });
    return momento
}

console.log(lastSave('./Data.xlsx'))  // this writes "undefined"

Use the sync variant of the function.使用 function 的同步变体。

const fs = require('fs');

const lastSave = file => {
    return fs.statSync(file).mtime;
}

console.log(lastSave('./Data.xlsx'));

If there is no sync version wrap the call in a async function and call it:如果没有同步版本,则将调用包装在异步 function 中并调用它:

const fs = require('fs');

const lastSave = file => {
    return new Promise(done => fs.stat(file, function (err, stats) {
        var mtime = stats.mtime;
        console.log("interno", mtime);    // this works
        done(mtime);
    }));
}

(async () => {
    console.log(await lastSave('./Data.xlsx'));
})();

You can use the promise-based variant:您可以使用基于承诺的变体:

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

const lastSave = async file => {
    return (await fs.stat(file)).mtime;
}

(async () => {
    console.log(await lastSave('./Data.xlsx'));
})();

Alternatively you can promisify your async function:或者,您可以承诺您的异步function

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

const lastSave = async file => {
    return (await util.promisify(fs.stat)(file)).mtime;
}

(async () => {
    console.log(await lastSave('./Data.xlsx'));
})();

Alternatives for替代品

(async () => {
    console.log(await lastSave('./Data.xlsx'));
})();

are

lastSave('./Data.xlsx').then(console.log);

and

lastSave('./Data.xlsx').then(result => { console.log(result); });

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

相关问题 如何在 Array.filter 中传递异步回调函数? - How can I pass async callback function in Array.filter? 我可以将异步函数作为回调传递给异步函数吗? - Can I pass an async function as a callback to an async function? 如何将值传递给 javascript function? - How can I pass a value to a javascript function? 如何在JS中将值传递给异步函数 - How to pass a value to async function in JS 如何将异步函数值传递给事件处理程序 - How to pass async function value to an event handler 如何将选择值传递给另一个函数,并根据传递的值从Json获取数据? - How can I pass select value to another function and get data from Json according to passed value? 如何编写一个 function,我可以在其中将枚举的键作为字符串传递并获取映射到它的值? 使用 angular 11 - How to write a function where I can pass the key of the enum as a string and get the value mapped to it? Using angular 11 在异步函数上调用 then 之后如何获取异步函数的返回值 - how to get the return value of an async function after then is called on the async function 如何将变量传递给使用 Puppeteer 评估异步 function 的评估 function? - How can I pass variable into an evaluate function that evaluates an async function using Puppeteer? 如何在不使用函数参数的情况下将值传递给函数? - How can I pass a value into a function without using function parameters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM