将 Sharp Promise 保存到变量返回待处理
我正在尝试根据 Base64 编码字符串调整图像大小。我可以在 nodejs
中使用 c 来完成此操作。 Sharp
与 Promises 一起使用,所以我知道我必须执行异步方法。调整大小后,我需要将其转换回 Base64 字符串。我通常通过使用 nodejs Buffer
将图像转换为缓冲区来实现此目的,然后使用 'toString()
方法将其转换回 Base64。
我的代码如下所示:
async function resizer(base64, width = 224, height = 224) {
if (!base64) {
throw console.error("not a base64 string")
} else {
const Buffer = require("buffer").Buffer
let base64buffer = Buffer.from(base64, "base64")
const image = await sharp(base64buffer)
.resize({
width: width,
height: height,
fit: sharp.fit.cover,
})
.rotate(90)
.toBuffer()
const newBase64 = image.toString("base64")
return newBase64
}
}
const resizedBase64 = resizer(base64Image).then((result) => {
console.log(result)
return result
})
console.log(resizedBase64)
该函数采用 base64 字符串作为参数。然后它被转换为缓冲区,我让 Sharp 做这件事。然后我返回新生成的字符串。然而,当我将其存储为变量时,它返回一个 Promise {pending}
。当我只是在 .then()
回调中使用 console.log 时,它确实会记录新的 base64 字符串。
有人知道如何解决这个问题吗?我稍后需要在代码中使用新的 base64。
先感谢您!
I'm trying to resize an image from a base64 encoded string. I can do this using c in nodejs
. Sharp
works with Promises, so I know I have to do async methods. After resizing, I need to convert it back to a base64 string. I usually do this by converting an image to a buffer, using the nodejs Buffer
, then use the 'toString()
method to convert it back to base64.
This is what my code looks like:
async function resizer(base64, width = 224, height = 224) {
if (!base64) {
throw console.error("not a base64 string")
} else {
const Buffer = require("buffer").Buffer
let base64buffer = Buffer.from(base64, "base64")
const image = await sharp(base64buffer)
.resize({
width: width,
height: height,
fit: sharp.fit.cover,
})
.rotate(90)
.toBuffer()
const newBase64 = image.toString("base64")
return newBase64
}
}
const resizedBase64 = resizer(base64Image).then((result) => {
console.log(result)
return result
})
console.log(resizedBase64)
The function takes a base64 string as an argument. It then gets converted to a buffer and I let sharp do it's thing.I then return the the newly generated string. When I store it as a variable, however, it returns a Promise {pending}
. When I just console.log inside the .then()
callback, it does log the new base64 string.
Anyone know how to fix this? I need to use the new base64 later in the code.
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需添加一个
await
即可从 Promise 中获取解析值:编辑:这里需要理解的重要一点是您的
resizer
函数包含一些异步代码(来自 Sharp库)返回一个承诺。因此,您的函数也将始终返回值的承诺,而不是值本身,即使您在其中使用await
也是如此。函数的调用者将收到承诺,并且必须等待它才能获取其实际值。调用函数
resizer
时,如果您不在异步上下文中且无法使用await
,则有几种可用的解决方案。解决方案 1:使用 IIFE
解决方案 2:声明一个异步函数(与之前的基本相同)
解决方案 3:将代码放在
then
回调中You simply need to add an
await
to get the resolved value from the promise:EDIT: What's important to understand here is that your
resizer
function contains some asynchronous code (from the Sharp library) which returns a promise. As such, your function will also always return a promise of the value, not the value itself, even if you useawait
inside it. The caller of your function will receive the promise and will have to await it to get its actual value.When calling your function
resizer
, if you are not in an async context and cannot useawait
there are a couple of solutions available.Solution 1: use an IIFE
Solution2: declare an async function (basically the same as previous)
Solution 3: place your code inside the
then
callback