gpt4 book ai didi

javascript - 如何将变量传递给 Promise 链中调用的另一个文件中的异步函数

转载 作者:搜寻专家 更新时间:2023-11-01 00:47:30 26 4
gpt4 key购买 nike

如何将变量传递给 promise 中另一个文件中的异步函数?

// file1.js

const thisFunc = require('./file2');
const foo = "bar";

const newPromise = new Promise((resolve, reject) => {
thisFunc
.asyncFunction() // <-- I want to pass foo here
.then(...)
}
// file2.js

const asyncFunction = async () => {
console.log(foo); // <-- and do stuff with foo here
}

module.exports.asyncFunction = asyncFunction

最佳答案

像传递任何其他变量一样传递变量 - 异步事件不会阻止您这样做:

thisFunc
.asyncFunction(foo)
.then(...)

然后在asyncFunction中添加一个参数:

const asyncFunction = async foo => {...};

关于javascript - 如何将变量传递给 Promise 链中调用的另一个文件中的异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57581722/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com