gpt4 book ai didi

javascript - Node js,函数不等待响应

转载 作者:行者123 更新时间:2023-12-03 06:42:17 25 4
gpt4 key购买 nike

我是Node js的新手。
从逻辑上讲,await应该等待函数完成,然后下一行代码应该运行,但是在我的函数中,即使使用await之后,它也不等待进程(2)完成。我想念什么?

const getDefaultTemplates = async (token) => {
const emailertoken = process.env.EMAILER_AUTH_TOKEN;
const key = 'defaultTemplates';
// This should run first
console.log('hi');
let defaultTemplatesVar = '';
let fromCache = 0;

// This should run second. Need response from redisClient to return parent function
const defaultTemplates = await redisClient.get(key, (err, data) => {
if (err) {
console.log(err);
}
// If data is found in cache
if (data != null) {
console.log('from cache');
defaultTemplatesVar = JSON.parse(data);
fromCache = 1;
console.log('defaultTemplatesVar1 = ', defaultTemplatesVar);
}
return defaultTemplatesVar;
});

console.log('defaultTemplatesVar2 = ', defaultTemplatesVar);
console.log('fromCache = ', fromCache);
if (fromCache === 0) {
// If data is not found in cache, call api
// console.log('from api');
try {
const response = await axios.get(`${process.env.EMAILER_API_URL}/get-system-templates`, {
headers: {
Authorization: `bearer ${emailertoken}`,
},
});
console.log('from data');
redisClient.setex(key, 3600, JSON.stringify(response.data._embedded.get_system_templates));
defaultTemplatesVar = response.data._embedded.get_system_templates;
console.log('defaultTemplatesVar3 = ', defaultTemplatesVar);
} catch (error) {
console.error(error);
}
}

// This should run at last, to return value to parent function getDefaultTemplates()
console.log('bye');
console.log('defaultTemplatesVar4 = ', defaultTemplatesVar);
return defaultTemplates;
};


输出->
hi
defaultTemplatesVar2 =
fromCache = 0
from cache
defaultTemplatesVar1 = [ { name: 'versafix-1', description: 'The versatile template' },
{ name: 'givecentral', description: 'The Givecentral Template' } ]
from data
defaultTemplatesVar3 = [ { name: 'versafix-1', description: 'The versatile template' },
{ name: 'givecentral', description: 'The Givecentral Template' } ]
bye
defaultTemplatesVar4 = [ { name: 'versafix-1', description: 'The versatile template' },
{ name: 'givecentral', description: 'The Givecentral Template' } ]
输出应为1,2,3,4的串联。

最佳答案

回调函数不能为await
要将回调fn转换为Promise fn,请检查https://www.npmjs.com/package/redis#promises
如果您使用的是其他软件包,则实际上可以通过

const redisGet = (key) => new Promise((resolve, reject) => {
redisClient.get(key, (err, data) => {
if (err) return reject(err);
return resolve(data);
});
});

// So you can do
const data = await redisGet(key);
// or
redisGet(key).then((data) => { ... }).catch((err) => { ... });
所以你应该怎么做
try {
...

const data = await redisClient.get(key);

if (data != null) {
console.log('from cache');
defaultTemplatesVar = JSON.parse(data);
fromCache = 1;
console.log('defaultTemplatesVar1 = ', defaultTemplatesVar);
}
const defaultTemplates = defaultTemplatesVar;
...
} catch (err) {
console.error(err);
}
关于 Callback vs Promise的简单教程

关于javascript - Node js,函数不等待响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62949434/

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