[英]Awaiting doesn't appear to be waiting for results in my functions
I realize this sort of question has been asked here, and I have done extensive research before I posted this question.我意识到已经在这里提出了此类问题,并且在发布此问题之前我已经进行了广泛的研究。
I have seven operations that need to be run in order.我有七个需要按顺序运行的操作。 Most are database related, so they may take some time to finish up.
大多数与数据库相关,因此可能需要一些时间才能完成。
getLiveItems => puppeteer page (avg 10 seconds) getLiveItems => puppeteer 页面(平均 10 秒)
getOldItems => mySQL query getOldItems => mySQL 查询
getNewItems => mySQL query getNewItems => mySQL 查询
compareItems => lodash compare compareItems => lodash 比较
saveOldItems => mySQL insert query saveOldItems => mySQL 插入查询
saveNewItems => mySQL insert query saveNewItems => mySQL 插入查询
notifyUser => puppeteer open page notifyUser => puppeteer 打开页面
I understand I need to either use a callback, or a promise.我知道我需要使用回调或 promise。 But I can't even get past the first two operations using either method.
但我什至无法使用任何一种方法完成前两个操作。
Here is my latest attempt.这是我最近的尝试。
var oldItems, newItems;
var getoldItems = async function(callback) {
await connection.query('SELECT * FROM items', function(err, rows) {
if (err) {
throw err;
} else
callback(null, rows);
});
}
var getnewItems = async function(callback) {
await connection.query('SELECT * FROM newItems', function(err, rows) {
if (err) {
throw err;
} else
callback(null, rows);
});
}
const init = async function(){
await getoldItems(function(err, content) {
if(err){
console.log(err);
} else {
oldItems = content;
return oldItems;
}
})
await getnewItems(function(err, content) {
if(err){
// console.log(err);
} else {
newItems = content;
return newItems;
}
})
return;
}
( async function() {
await init();
console.log(oldItems);
console.log(newItems);
});
I get no output.我没有得到 output。 No errors, either.
也没有错误。
I'm really confused.我真的很困惑。 I have read, and read about this topic, but I am missing something fundamental.
我已经阅读并阅读了有关此主题的内容,但我缺少一些基本的东西。
This is how my mind is processing my script, thus the reason I wrote it that way...这就是我的大脑处理我的脚本的方式,因此我这样写它的原因......
I am going to await the results from the init function, which does the following...
我将等待 init function 的结果,它执行以下操作...
I am going to wait for the results of getOldItems and then move on to waiting for the results of getNewItems.
我将等待 getOldItems 的结果,然后继续等待 getNewItems 的结果。 Then, I am going to return back to Init and console out both results.
然后,我将返回到 Init 并控制台输出这两个结果。
What Am I Missing?!我错过了什么?!
Thank you in advance.先感谢您。
Frustrated In Florida, John在佛罗里达沮丧,约翰
You're using callbacks, not Promises, so your awaits aren't actually doing anything.您使用的是回调,而不是 Promises,因此您的 await 实际上并没有做任何事情。
You have a few choices: - switch to all callbacks - wrap your callbacks with Promises - use a db library that supports Promises natively你有几个选择: - 切换到所有回调 - 用 Promises 包装你的回调 - 使用原生支持 Promises 的数据库库
A few Promise-enabled mysql libraries:一些支持 Promise 的 mysql 库:
https://www.npmjs.com/package/mysql2 https://www.npmjs.com/package/mysql2
https://www.npmjs.com/package/promise-mysql https://www.npmjs.com/package/promise-mysql
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.