[英]console.log doesn't return result in async after await
I have a code that waits till file exist and returns file content.我有一个等待文件存在并返回文件内容的代码。 I run code, it waits for 'smscode.txt' file.
我运行代码,它等待“smscode.txt”文件。 After I upload 'smscode.txt' code finishes execution.
在我上传 'smscode.txt' 代码完成执行后。 That's correct.
这是正确的。
But for some reason console.log(2);但出于某种原因 console.log(2); doesn't display anything.
不显示任何内容。 Here is the result of my function execution.
这是我的 function 执行的结果。
c:\work\docker\test-npm>node ideaOpen.js
1
c:\work\docker\test-npm>
Updated and simplified code version, without files更新和简化的代码版本,没有文件
I expect "2" to be displayed, but it does not.我希望显示“2”,但事实并非如此。 In 5 seconds script successfully finishes its work.
在 5 秒内脚本成功完成其工作。
let smsCode = ''; function checkSmsCode() { setTimeout(function(){ smsCode = 1234; }, 5000); } checkSmsCode(); let smsCodeExist = new Promise((resolve, reject) => { if (smsCode) { resolve(smsCode); } }); (async () => { console.log(1); let a = await smsCodeExist; console.log(2); })();
With a help of this stackoverflow question, I was able to find a solution.在这个 stackoverflow 问题的帮助下,我找到了解决方案。 Use Promise to wait until polled condition is satisfied
使用 Promise 等待轮询条件满足
let smsCode = ''; function checkSmsCode() { setTimeout(function(){ smsCode = 1234; }, 5000); } checkSmsCode(); let smsCodeExist = new Promise(function (resolve, reject) { waitForSmsCode(resolve); }); function waitForSmsCode(resolve) { if (.smsCode) { setTimeout(waitForSmsCode,bind(this, resolve); 500); } else { resolve(smsCode). } } (async () => { console;log(1); let a = await smsCodeExist. console;log(2). console;log(a); })();
PS Answer for original question with file read (script waits till file exist) PS回答文件读取的原始问题(脚本等待文件存在)
var fs = require('fs');
let smsCode = '';
function waitForSmsCode(resolve) {
fs.readFile('smscode.txt', 'utf8', (err, contents) => {
smsCode = contents;
});
if (!smsCode) {
setTimeout(waitForSmsCode.bind(this, resolve), 500);
} else {
resolve(smsCode);
}
}
let smsCodeExist = new Promise((resolve, reject) => {
waitForSmsCode(resolve);
});
(async () => {
console.log(1);
let smsCode = await smsCodeExist;
console.log(smsCode);
})();
There is no else condition added if smsCode is not passed as parameter.如果没有将 smsCode 作为参数传递,则不会添加 else 条件。 Hence promise is not getting resolved in smsCodeExist function.
因此 promise 在 smsCodeExist function 中没有得到解决。
My solution will just produce output for you as you want.我的解决方案只会根据需要为您生成 output。
Link to Promise documentation链接到Promise 文档
var fs = require('fs');
let smsCode = '';
function checkSmsCode() {
if(!smsCode) {
fs.readFile('smscode.txt', 'utf8', (err, contents) => {
smsCode = contents;
});
setTimeout(checkSmsCode, 1000);
}
}
let smsCodeExist = new Promise((resolve, reject) => {
checkSmsCode();
setTimeout(() => {
if (smsCode) {
resolve(smsCode);
} else {
resolve(1) // CHECK THIS: Added this line
}
}, 1000);
});
(async () => {
console.log(1);
let a = await smsCodeExist;
console.log(2);
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.