【发布时间】:2021-12-08 20:50:26
【问题描述】:
我做了一些数据库操作和一些肥皂电话。这是结果。这对我来说似乎不正确。如何在 nodejs 中同步它们?这都是异步函数。
这里是代码
sccmConnectDB.getAllComputers().then(result => {
computers = result[0]
console.log("select from sccm db is completed...")
caConnectDB.deletezSCCM2CM().then(result => {
console.log("deleting from ca db zsccm2cm table is completed...");
caConnectDB.insertzSCCM2CM(computers).then(result => {
console.log("inserting ca db zsccm2cm table is completed...");
login.login().then(sessionId => {
console.log("login successfull");
getHostName.getHostNames().then(result => {
hostNames = result[0];
console.log("query result for hostname -->", hostNames);
hostNames.forEach(element => {
uuid = "nr:" + element.uuid;
attrVals = { string: ["system_name", element.compName] };
attrbts = { Attributes: [] }
updateObject.updateObject(sessionId, uuid, attrVals, attrbts).then(result => {
console.log("Update successfull");
});
});
logout.logout(sessionId).then(result => {
console.log("logout successfull");
});
});
});
});
});
});
【问题讨论】:
-
make sync the async,,你不能......一旦某件事是异步的,链上的所有东西也必须是异步的。但是 nodejs 确实有 async/await,所以可以让这变得更容易。 -
我想你误解了我的问题。我已经做了异步功能。但我想让它们都同步工作。因为当第一个异步函数被解析时,它必须启动第二个异步函数,然后解析,然后是第三个异步函数..等等。
-
但这就像在问,“大家好,我已经制造了一辆赛车,它非常高效并且赢得了所有比赛。请告诉我如何破坏它,让它变得更重、更笨重而且效率低下,所以它输掉了所有的比赛。因为原因。谢谢”
-
那么你认为这是我的代码的完美版本吗?我之前没有看到 then->then->then->then 的用法。我是 nodejs 的新手 :)
-
then->then->then->then然后你错过了关于我说有async / await的那一点,这就是它的原因,它不是同步编码的直接替代品,但在语法方面它使它看起来非常接近.. 例如,您也需要替换那个 forEach,但这并不太激烈..
标签: javascript node.js asynchronous promise synchronization