Javascript 如何按顺序执行java脚本代码?
每天我都在学习javascript(nodejs)中的新东西,但是有一件事让我很困惑,那就是异步等待的概念 需要一些帮助来管理下面代码中的执行顺序。下面是它应该如何执行 我正在写一个每日脚本,它应该从本地系统读取文件值(文件包含:上次运行日期)。 读取完文件后,在文件中放入新值(文件应存储当前日期) 输出:Javascript 如何按顺序执行java脚本代码?,javascript,node.js,asynchronous,async-await,Javascript,Node.js,Asynchronous,Async Await,每天我都在学习javascript(nodejs)中的新东西,但是有一件事让我很困惑,那就是异步等待的概念 需要一些帮助来管理下面代码中的执行顺序。下面是它应该如何执行 我正在写一个每日脚本,它应该从本地系统读取文件值(文件包含:上次运行日期)。 读取完文件后,在文件中放入新值(文件应存储当前日期) 输出: Step 1 : Today's Date: 4 Step 4 : Finally Saving the todays date in last run file: Step 5 : Sav
Step 1 : Today's Date: 4
Step 4 : Finally Saving the todays date in last run file:
Step 5 : Saved the record in file
Step 2 :Last run was done on(MM/DD/YYYY): Fri Dec 04 2020 13:10:01 GMT+0530 (India Standard Time)
Step 3 :Its been 1 day(s) Since the last run
预期产出:
Step 1 : Today's Date: 4
Step 2 :Last run was done on(MM/DD/YYYY): Fri Dec 04 2020 13:06:26 GMT+0530 (India Standard Time)
Step 3 :Its been 1 day(s) Since the last run
Step 4 : Finally Saving the todays date in last run file:
Step 5 : Saved the record in file
wait
仅对承诺有用
但是fs.readFile
返回未定义的
,因此在其上使用它没有任何意义
如果您使用VSC,您可以将鼠标悬停在上面,看到它返回void
,这意味着什么也没有,这意味着未定义
您可以做的是提示readFile
和writeFile
。为此,您可以使用newpromise()
此函数将返回一个承诺。您可以使用res
进行解析,也可以使用rej
现在你可以看到它回报了一个承诺
一旦出现错误,您将拒绝,否则您将解决承诺
同样适用于readFile
function read(filePath) {
return new Promise((res, rej) => {
fs.readFile(
filePath,
{
encoding: "utf-8"
},
function(err, data) {
if (!err) {
console.log("Step 2 :Last run was done on(MM/DD/YYYY): " + data);
let lastrunDate = new Date(data);
const diffTime = Math.abs(todaysDate - lastrunDate);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
console.log(
"Step 3 :Its been " + diffDays + " day(s) Since the last run"
);
res(data);
} else {
rej(err);
}
}
);
});
}
这里我解析数据
。稍后当我们使用let data=wait read(…)
时,它将返回我们的数据。
现在,这两个函数都返回一个挂起的承诺
现在,在我们的函数中使用wait
是有意义的
async function checkPreviousRunDate() {
console.log("Step 1 : Today's Date: " + todaysDate.getDate());
filePath = path.join(__dirname, "lastrundetail.txt");
let data = await read(filePath);
console.log(data);
}
async function storeTodaysDateinLastRunFile() {
console.log("Step 4 : Finally Saving the todays date in last run file: ");
await write(todaysDate);
return todaysDate;
}
如果发生错误,您可以将其包装在通常应该执行的try/catch
中。这将捕获rej
错误
async function checkPreviousRunDate() {
console.log("Step 1 : Today's Date: " + todaysDate.getDate());
filePath = path.join(__dirname, "lastrundetail.txt");
try {
let data = await read(filePath);
console.log(data);
catch (err) {
console.log(err);
}
}
fs.readFile
不会返回承诺,因此,您不能等待它-这同样适用于fs.writeFile
您可能应该使用而不是回调文件wait fs.readFile
不会真正等待整个文件读取完成。这是否回答了您的问题?
async function checkPreviousRunDate() {
console.log("Step 1 : Today's Date: " + todaysDate.getDate());
filePath = path.join(__dirname, "lastrundetail.txt");
let data = await read(filePath);
console.log(data);
}
async function storeTodaysDateinLastRunFile() {
console.log("Step 4 : Finally Saving the todays date in last run file: ");
await write(todaysDate);
return todaysDate;
}
async function checkPreviousRunDate() {
console.log("Step 1 : Today's Date: " + todaysDate.getDate());
filePath = path.join(__dirname, "lastrundetail.txt");
try {
let data = await read(filePath);
console.log(data);
catch (err) {
console.log(err);
}
}