node.js - 退出前等待响应
问题描述
我正在调用这个函数,它会将图像上传到 API 调用,然后它会返回一个远程图像的 url。
hash_image = putImageOnCloud(id_kromeda, filename)
.then(
(value) => {
console.log('Immagine salvata in cloud: ' + value);
process.exit(6);
})
process.exit(5);
return hash_image;
但它以没有打印值的代码 5 退出;
所以我需要等待请求响应。我怎么能这样做?
async function putImageOnCloud(id_kromeda, filename) {
filename = __dirname + '/../../public/images/' + filename;
console.log('Salvo l\'immagine ' + filename + ' nel cloud.');
let options = {
'method': 'POST',
'url': 'http://localhost/api/endpoint',
'headers': {},
formData: {
'file': {
'value': fs.createReadStream(filename),
'options': {
'filename': '__.jpg',
'contentType': null
}
}
}
};
request(options, function (error, response) {
console.log('Faccio la call..')
if (error) {
throw new Error(error);
process.exit(4);
}
console.log(response.body);
let risposta = JSON.parse(response.body);
/*
[
{
"file_url": "http://localhost/a6e10970-c492-42f7-bc0d-965d28be7d2b.jpg",
"_id": "a6e10970-c492-42f7-bc0d-965d28be7d2b.jpg"
}
]
*/
let image_cdn = risposta[0]._id; // file_url
console.log('[' + id_kromeda + '] Salvato immagine in cloud: ' + image_cdn);
return image_cdn;
});
}
解决方案
推荐阅读
- javascript - 想要将数据存储在 reactJs 中以便更快地访问
- python - 找到一个充满给定项目的子列表并替换它
- php - 当值返回产品帖子数组时,查询 `meta_key` 的帖子标题,然后显示搜索词与链接的帖子标题匹配的帖子
- python - 我可以在 nn.Sequential 中解压一个 nn.ModuleList 吗?
- python - 在 Python 3 Windows 中与打印机交互的最佳方式是什么?
- spring-boot - 您可以从带注释的类生成二级索引 DDL 语句吗?
- microsoft-graph-api - Microsoft Graph - 获取消息 mime 时获取无效 ID
- android - Settings.Global.LOW_POWER_MODE 和 Settings.Global.LOW_POWER_MODE_STICKY 在 android 11 中获得低功耗模式的替代方法是什么?
- android-studio - 如何在 Android 上运行 JS 库
- alarm - 是否可以将数据添加到警报的详细信息中?