Javascript 使用electron返回多个回调函数中的值时出错
我目前正试图从electron json存储数据库中获取一个值并返回它Javascript 使用electron返回多个回调函数中的值时出错,javascript,callback,electron,puppeteer,Javascript,Callback,Electron,Puppeteer,我目前正试图从electron json存储数据库中获取一个值并返回它 函数getWithExpiry(键){ var=”; 存储.has(键,函数(错误,hasKey){ 如果(错误)抛出错误; 如果(hasKey){ 获取(键、函数(错误、数据){ 如果(错误)抛出错误 const item=JSON.parse(数据) const now=新日期() if(now.getTime()>item.expiry){ 存储。删除(键、功能)(错误){ 如果(错误)抛出错误 参数=null })
函数getWithExpiry(键){
var=”;
存储.has(键,函数(错误,hasKey){
如果(错误)抛出错误;
如果(hasKey){
获取(键、函数(错误、数据){
如果(错误)抛出错误
const item=JSON.parse(数据)
const now=新日期()
if(now.getTime()>item.expiry){
存储。删除(键、功能)(错误){
如果(错误)抛出错误
参数=null
})
}否则{
console.log(item.value)
值=项.value
}
})
}
})
控制台日志(抓取)
回抓;
}
我这样称呼它:
setTimeout(异步()=>{
var tokenpass=getWithExpiry('captcha'))
console.log(令牌传递)
等待page.evaluate((tokenpass)=>{document.getElementById(“g-recaptcha-response”)。innerText=`${tokenpass}`},tokenpass)
等待页面。单击('input.button')。然后(console.log('Clicked'));
console.logs返回的顺序与调用的顺序不同。首先,它在getWithExpiry(key)
中从console.log(抓取)
返回“”,然后从console.log(tokenpass)
返回“”,然后从console.log('Clicked')
返回预期值(item.value)。我认为这些控制台.log的顺序可能是getWithExpiry
不返回item.value
的原因
放置等待页面。等待(1000);
因为当您更改InInterText时,更新元素需要一些时间
async function getWithExpiry(key) {
return new Promise((res, rej) => {
storage.has(key, function (error, hasKey) {
if (error) rej(error);
if (hasKey) {
storage.get(key, function (error, data) {
if (error) rej(error);
const item = JSON.parse(data);
const now = new Date();
if (now.getTime() > item.expiry) {
storage.remove(key, function (error) {
if (error) rej(error);
res(null);
});
} else {
console.log(item.value);
res(item.value);
}
});
}
});
});
}
setTimeout(async () => {
var tokenpass = await getWithExpiry("captcha");
console.log(tokenpass);
await page.evaluate((tokenpass) => {
document.getElementById("g-recaptcha-response").innerText = `${tokenpass}`;
}, tokenpass);
await page.waitFor(1000);
await page.click("input.button").then(console.log("Clicked"));
});
固定顺序,但现在console.log(tokenpass)
正在返回Promise{::}。
仍以“”而不是项.value退出函数。