【发布时间】:2018-11-14 17:21:11
【问题描述】:
我知道这个问题与这个问题几乎相同:Execution order of Promises 但有人可以向我解释我的错误在哪里吗? 我有下一个功能:
// The main function
function startTesting() {
console.info("--- Thanks! Testing is running... ---");
checkFolderExistence(dirPath)
.then(checkFolderContent)
.then(searchForImportFolder)
.then(connectToDB)
.catch(err => console.error("*** ERROR *** " + err));
}
function checkFolderExistence(path) {
console.info('--- Checking the folder "' + path + '" existence... ---');
let promise = new Promise(function(resolve, reject) {
fs.readdir(path, (err) => {
if(err) {
console.error('*** ERROR **** The folder "C:\\For_testing" doesn\'t exist. Testing is stopped!!! ***');
} else {
console.info("--- The folder \"C:\\For_testing\" exists... ---");
resolve(path);
};
});
});
return promise;
}
function checkFolderContent(path) {
console.info('--- Checking the folder "' + path + '" content... ---');
filesArray = fs.readdirSync(path);
if(filesArray.length == 0) {
console.error('*** ERROR *** There are no any files in ' + path + '. Testing is stopped!!! ***');
} else {
console.info('--- The folder is checked. It contains the next files: ---');
for(let i = 0; i < filesArray.length; i++) {
console.info(filesArray[i]);
}
};
}
function searchForImportFolder() {
console.info('--- Searching for ".../Import" folder... ---');
fs.readdir(destFolderPath64, (err) => {
if(err) {
fs.readdir(destFolderPath32, (err) => {
if(err) {
console.error('*** ERROR *** The folder ".../Import" was not found ***');
} else {
console.info('--- The folder ".../Import" was successfully found... ---');
trueDestPath = destFolderPath32;
}
});
} else {
console.info('--- The folder "C:/Program Files (x86)/StoreLine/Office/Import" was successfully found... ---');
trueDestPath = destFolderPath64;
}
});
}
function connectToDB() {
console.info('--- Connecting to the database... ---');
let pool = new sql.ConnectionPool(config);
pool.connect()
.then(pool => {
console.info("--- Connected to the database! ---");
readDB(pool)
.then(function() {
console.info("--- All needed information from DB was successfully received ---");
})
.catch(err => console.error("*** ERROR *** " + err));
})
.catch(err => {
pool = new sql.ConnectionPool(configWithoutPassw);
pool.connect()
.then(pool => {
console.info("--- Connected to the database without the password! ---");
readDB(pool)
.then(function() {
console.info("--- All needed information from the DB was successfully received ---");
})
.catch(err => console.error("*** ERROR ***" + err));
})
.catch(err => {
console.error("*** ERROR *** Can't connect to the DB ***")
sql.close();
});
});
}
我需要严格的函数执行顺序:checkFolderContent => searchForImportFolder => connectToDB。
实际上执行是下一个:checkFolderContent 完全执行,然后searchForImportFolder 开始执行(我可以看到行“--- Searching for ".../Import" folder... --- “在控制台中),但紧接着connectToDB 开始并出现下一行“--- Connecting to the database... ---”。在该行之后,我从上一个函数中看到“--- 成功找到文件夹“.../Import”... ---”。
我做错了什么?我读过.then() 函数应该返回一个承诺。我该怎么做?
【问题讨论】:
-
使用异步等待来维护脚本执行的顺序
-
您的
searchForImportFolder是异步的,但不返回承诺。因此,下一个then立即执行,返回值为searchForImportFolder,即undefined,因为没有返回任何内容;trueDestPath被设定在未来的某个时间,被忽视和悲伤。 -
@MohamedAshiff -
async/await在searchForImportFolder的基本问题得到解决之前将无济于事。 -
你也可以chain你的承诺。
-
@lependu - OP 正在兑现他的承诺。
标签: javascript node.js promise