javascript - Jimp.read never runs callback and hangs up
问题描述
I'm trying to make a Discord bot, and one of my commands resizes a requested image and sends it. When I run the command though, it just hangs up with no error message. If I take out the callback and run it normally, it sends, but sends the last image requested instead of the one that just was.
function resizeImg(id,_callback) {
const img = Jimp.read('image.png')
.then(img => {
return img
.resize(100,130)
.write('temp.png');
_callback();
})
.catch(console.error);
}
resizeImg(id,function() {
var attachment = new Discord.MessageAttachment('./temp.png')
msg.channel.send('',attachment)
});
解决方案
return
由于停止执行的语句,您永远不会达到回调。
function resizeImg(id, _callback) {
Jimp.read('image.png').then(img => {
const img = img.resize(100,130).write('temp.png');
_callback(img);
})
.catch(console.error);
}
resizeImg(id, function(img) {
var attachment = new Discord.MessageAttachment('./temp.png')
msg.channel.send('', attachment)
});
试试上面的代码。您现在可以通过回调中的参数访问 img。
推荐阅读
- sdl-2 - 如何配置 SDL_image 以使用对等目录中的 SDL 版本?
- onem2m - OneM2M 中条件检索的 QueryString 结构?
- ios - 切换视图控制器后第二次调用方法的问题
- indy - 使用 BytesToInt64() 将 TIdBytes 转换为 Integer 的意外结果
- python - 根据另一个列值创建一个 pandas 数据框列
- php - 找不到包 php7.3-gd
- computer-vision - Google colab:RuntimeError:后端 CPU 的预期对象,但为参数 #4 'mat1' 获得了后端 CUDA
- biztalk - BizTalk - 绑定属性 -“useAmbientTransaction” = False
- oracle - 如何在 oracle apex 中取消隐藏页面项目
- sql - Hive 排序数组列相对于同一表中的其他数组列