如何使用 express 获取异步方法的输出值作为输入和显示
How do I get the output value of an async method as input and display using express
我对node和express有点陌生
我正在尝试构建一个 API 消耗外部 API 和 returns 结果作为我显示内容的输入
如何使用快速发送方法获取异步方法的输出值作为输入和显示
查看下面我的快递代码
const express = require("express");
var AsyncMethod = require("./src/AsyncMethod");//works perfectly
const app = express();
const port = 8000;
app.get("/url", (req, res) => {
var output = "";
var biller = new AsyncMethod();
try {
output = await (biller.run());
.then(//need help here
console.log("inside Main")
res.send(output))
} catch (ex) {
console.log("An error occurred");
res.send("An error occurred");
console.log(ex);
}
});
您需要使用 .then()
方法和 return 回调的响应等待异步方法执行完成。
使用Async/Await
const express = require("express");
var AsyncMethod = require("./src/AsyncMethod");//works perfectly
const app = express();
const port = 8000;
app.get("/url", async (req, res) => {
var output = "";
var biller = new AsyncMethod();
try {
output = await biller.run();
res.send(output)
} catch (ex) {
console.log("An error occurred");
res.send("An error occurred");
console.log(ex);
}
});
使用promise
const express = require("express");
var AsyncMethod = require("./src/AsyncMethod");//works perfectly
const app = express();
const port = 8000;
app.get("/url", (req, res) => {
var biller = new AsyncMethod();
biller.run().then(function(output){
res.send(output)
}).catch(function(ex){
console.log("An error occurred");
res.send("An error occurred");
console.log(ex);
})
});
我查看了您在评论中发布 link 的 github 项目
.run
需要 return 一个 Promise
如果你想 await
它
那么 - 这是如何做到这一点...另外 5 行标记为 //+++
var BaseSample = require('./BaseSample');
var GetBillers = function(){
//inherit
BaseSample.call(this);
}
GetBillers.prototype.run = function(){
return new Promise((resolve, reject) => { //+++
this.billpayment.get_billers(function(err, res){
if(err) {
//error executing request
console.log("Error calling get billers "+JSON.stringify(err));
reject(err); //+++
} else{
//check if it was successful
var statusCode = res.statusCode;
if(statusCode === 200) {//request was successful
var billerArray = JSON.parse(res.body).billers;
var firstBiller = billerArray[0];
var billerId = firstBiller.billerid;
var billername = firstBiller.billername;
console.log(billerId+" "+billername);
resolve(billerId+" "+billername); //+++
}
else{//it was not successful for a reason
console.log("FAILED: "+statusCode);
reject(statusCode); //+++
}
}
});
}); //+++
}
module.exports = GetBillers;
哦,你贴的代码应该有点像
const express = require("express");
var AsyncMethod = require("./src/AsyncMethod");//works perfectly
const app = express();
const port = 8000;
app.get("/url", (req, res) => {
var biller = new AsyncMethod();
try {
const output = await biller.run();
console.log("inside Main")
res.send(output))
} catch (ex) {
console.log("An error occurred");
res.send("An error occurred");
console.log(ex);
}
});
我对node和express有点陌生
我正在尝试构建一个 API 消耗外部 API 和 returns 结果作为我显示内容的输入
如何使用快速发送方法获取异步方法的输出值作为输入和显示
查看下面我的快递代码
const express = require("express");
var AsyncMethod = require("./src/AsyncMethod");//works perfectly
const app = express();
const port = 8000;
app.get("/url", (req, res) => {
var output = "";
var biller = new AsyncMethod();
try {
output = await (biller.run());
.then(//need help here
console.log("inside Main")
res.send(output))
} catch (ex) {
console.log("An error occurred");
res.send("An error occurred");
console.log(ex);
}
});
您需要使用 .then()
方法和 return 回调的响应等待异步方法执行完成。
使用Async/Await
const express = require("express");
var AsyncMethod = require("./src/AsyncMethod");//works perfectly
const app = express();
const port = 8000;
app.get("/url", async (req, res) => {
var output = "";
var biller = new AsyncMethod();
try {
output = await biller.run();
res.send(output)
} catch (ex) {
console.log("An error occurred");
res.send("An error occurred");
console.log(ex);
}
});
使用promise
const express = require("express");
var AsyncMethod = require("./src/AsyncMethod");//works perfectly
const app = express();
const port = 8000;
app.get("/url", (req, res) => {
var biller = new AsyncMethod();
biller.run().then(function(output){
res.send(output)
}).catch(function(ex){
console.log("An error occurred");
res.send("An error occurred");
console.log(ex);
})
});
我查看了您在评论中发布 link 的 github 项目
.run
需要 return 一个 Promise
如果你想 await
它
那么 - 这是如何做到这一点...另外 5 行标记为 //+++
var BaseSample = require('./BaseSample');
var GetBillers = function(){
//inherit
BaseSample.call(this);
}
GetBillers.prototype.run = function(){
return new Promise((resolve, reject) => { //+++
this.billpayment.get_billers(function(err, res){
if(err) {
//error executing request
console.log("Error calling get billers "+JSON.stringify(err));
reject(err); //+++
} else{
//check if it was successful
var statusCode = res.statusCode;
if(statusCode === 200) {//request was successful
var billerArray = JSON.parse(res.body).billers;
var firstBiller = billerArray[0];
var billerId = firstBiller.billerid;
var billername = firstBiller.billername;
console.log(billerId+" "+billername);
resolve(billerId+" "+billername); //+++
}
else{//it was not successful for a reason
console.log("FAILED: "+statusCode);
reject(statusCode); //+++
}
}
});
}); //+++
}
module.exports = GetBillers;
哦,你贴的代码应该有点像
const express = require("express");
var AsyncMethod = require("./src/AsyncMethod");//works perfectly
const app = express();
const port = 8000;
app.get("/url", (req, res) => {
var biller = new AsyncMethod();
try {
const output = await biller.run();
console.log("inside Main")
res.send(output))
} catch (ex) {
console.log("An error occurred");
res.send("An error occurred");
console.log(ex);
}
});