正如标题所述,如何从异步函数获取返回值?我需要提取返回值。 这是示例:
function callApi(){
return new Promise((resolve,reject)=>{
request({
url : url,
method:'POST'
},
function (error, response, body) {
if(response&&response.statusCode==200){
resolve(body);
}else{
reject("Error - -");
}
});
}
async function getApi(){
let jsonAll = {
data:[]
}
let json = await callApi();
jsonAll.data.push(json);
return jsonAll;
}
const express = require('express');
const app = express();
const assistant = require('ibm-watson/assistant/v1');
const service = assistant();
app.post('/sendMsg',function(req,res,err){
service.message({
workspace_id: '5d3caf46-9290-4047-b7bb-cb4c1969ee9a',
input: {'text': req.body.jsonDatas}
})
.then(response => {
##//how can I get JsonAll butnot the Promise object ??? ##
req.json();
})
.catch(error => {
console.log(error)
});
});
因此,在app.post和service.message中,如何获取“ jsonAll”? 请给我一个解决方案。非常感谢。