[英]C# async Task vs Js async Task (node js)
Ok, so I know how to program in C# fairly well and I have started programming in JS recently (node js).好的,所以我很清楚如何用 C# 编程,而且我最近开始用 JS 编程(node js)。 To be honest I was in a bot of shock from async calls.老实说,我对异步调用感到震惊。
Let's say I have this code in C#:假设我在 C# 中有这段代码:
var t_1 = SomeAsyncTask();
var t_2 = SomeOtherAsyncTask();
Task.WaitAll(t_1, t_2);
var res_1 = t_1.Result;
var res_2 = t_2.Result;
Is there a JS equivalent of this?有与此等效的 JS 吗? So far I have managed this:到目前为止,我已经做到了:
In User.js:
var express = require("express");
var router = express.Router();
var sqlDo = require("../../js_help/DatabasReq/sqlDo.js");
router.get("/", async function(req, res){
var json = sqlDo.ExecCommand("select * from Users");
res.send(json); //json.recordset
});
module.exports = router;
In sqlDo.js:
module.exports = {
ExecCommand: function(command){
// sql and config are defined before.
sql.connect(config, function () {
var request = new sql.Request();
request.query(command, function (err, recordset) {
if (err) console.log(err)
console.log(recordset.recordset);
return recordset;
});
});
}
};
My problem is that this code is running async.我的问题是这段代码正在异步运行。 I have tried putting await to different places but nothing worked.我试过将 await 放在不同的地方,但没有任何效果。 So when I start my server it returns nothing.所以当我启动我的服务器时,它什么都不返回。 I can tell that it is completing a call because I let it read results into console.我可以说它正在完成呼叫,因为我让它将结果读入控制台。
Thanks for any help!谢谢你的帮助!
Btw: I have tried googling/stackoverlow-ing,.. But I was not able to find anything that would look like C# equivalent.顺便说一句:我试过谷歌搜索/stackoverlow-ing,.. 但我找不到任何看起来像 C# 等效的东西。 Is it even possible to write it like in c#?甚至可以像在 c# 中那样编写它吗? Again thanks for every answer...再次感谢您的每一个回答...
To make your ExecCommand
function async, you have to make it return a Promise.要使您的ExecCommand
函数异步,您必须使其返回一个 Promise。 Read about Promises for instance here例如在这里阅读 Promises
module.exports = {
ExecCommand: function(command){
return new Promise((resolve, reject) => { //return a Promise from the function
sql.connect(config, function () {
var request = new sql.Request();
request.query(command, function (err, recordset) {
if (err) {
reject(err); //if there is an error, reject the Promise
} else {
resolve(recordset); //if no error, resolve the Promise with the result
}
});
});
});
}
};
Depending on your SQL library, it may also support promises already, instead of callbacks根据您的 SQL 库,它也可能已经支持承诺,而不是回调
module.exports = {
ExecCommand: function(command) {
return sql.connect(config)
.then(() => {
return new sql.Request().query(command);
})
}
};
or with async/await
或使用async/await
module.exports = {
ExecCommand: async function(command) {
await sql.connect(config);
return await new sql.Request().query(command);
}
};
Then you can call this function in the requesthandler either like this然后你可以像这样在请求处理程序中调用这个函数
router.get("/", async function(req, res){
try {
var json = await sqlDo.ExecCommand("select * from Users");
res.send(json);
catch (err) {
console.log(err);
res.sendStatus(500);
}
});
or like this或者像这样
router.get("/", function(req, res){
sqlDo.ExecCommand("select * from Users")
.then(json => { //the promise resolved
res.send(json);
})
.catch(err => { //the promise rejected
res.sendStatus(500);
console.log(err);
});
});
I prefer the second variant.我更喜欢第二种变体。 But that may be just my personal opinion ...但这可能只是我个人的看法......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.