【发布时间】:2020-11-27 05:17:29
【问题描述】:
我是节点 js 的新手。我已经使用了数据来自的 python 调用。由于该过程有点复杂,因此执行和节点 js 接收响应需要时间。我在这里提到的代码试图等待函数完成,但函数从头开始重复调用并一直重复直到 python 函数完成并获得响应。 所以功能完成后
console.log("Image Conversation::")
await bot.say(dumy_msg);
await bot.beginDialog('classfication_done_');
这些语句重复了两次。
如何解决问题?
convo1.ask('Post Your message', async(answer, convo1, bot,message) => {
if(message.type=='file_share') {
console.log("Image Conversation::")
const botToken = bot.api.token;
const destination_path = '/_classification/upload/' + message.files[0].name;
console.log("URl :::",message.files[0].url_private)
console.log('Testing file share' + message.files[0].name);
const url = message.files[0].url_private;
const opts = {
method: 'GET',
url: url,
headers: {
Authorization: 'Bearer ' + botToken,
}
};
request(opts, function (err, res, body) {
console.log('FILE RETRIEVE STATUS', res.statusCode);
}).pipe(fs.createWriteStream(destination_path));
const img = '/_classification/upload/' + message.files[0].name;
spawn = require("child_process").spawn;
const getResponse = () => {
return new Promise((resolve, reject) => {
const process = spawn('python',["/_classification/tensorflow-image-detection-master/classify.py",img] );
process.stdout.on('data', data => {
resolve(data);
});
});
}
const response = await getResponse();
var dumy_msg = `${response}`;
await bot.say(dumy_msg);
await bot.beginDialog('classfication_done_');
}
else{
await bot.say(message,"Please provide jpeg file format, other files are not allowed");
await bot.beginDialog('classfication_done_');
}
});
【问题讨论】:
标签: javascript python node.js asynchronous async-await