将 Sharp Promise 保存到变量返回待处理

发布于 2025-01-10 12:34:57 字数 1150 浏览 0 评论 0原文

我正在尝试根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千と千尋 2025-01-17 12:34:57

您只需添加一个 await 即可从 Promise 中获取解析值:

const resizedBase64 = await resizer(base64Image);

编辑:这里需要理解的重要一点是您的 resizer 函数包含一些异步代码(来自 Sharp库)返回一个承诺。因此,您的函数也将始终返回值的承诺,而不是值本身,即使您在其中使用 await 也是如此。函数的调用者将收到承诺,并且必须等待它才能获取其实际值。

调用函数 resizer 时,如果您不在异步上下文中且无法使用 await,则有几种可用的解决方案。
解决方案 1:使用 IIFE

(async() => {
  const resizedBase64 = await resizer(base64Image);
  console.log(resizedBase64);
  // the rest of your code goes here
})();

解决方案 2:声明一个异步函数(与之前的基本相同)

async main() => {
  const resizedBase64 = await resizer(base64Image);
  console.log(resizedBase64);
  // the rest of your code goes here
});

main();

解决方案 3:将代码放在 then 回调中

resizer(base64Image).then((resizedBase64) => {
  console.log(resizedBase64);
  // the rest of your code goes here
});

You simply need to add an await to get the resolved value from the promise:

const resizedBase64 = await resizer(base64Image);

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 use await 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 use await there are a couple of solutions available.
Solution 1: use an IIFE

(async() => {
  const resizedBase64 = await resizer(base64Image);
  console.log(resizedBase64);
  // the rest of your code goes here
})();

Solution2: declare an async function (basically the same as previous)

async main() => {
  const resizedBase64 = await resizer(base64Image);
  console.log(resizedBase64);
  // the rest of your code goes here
});

main();

Solution 3: place your code inside the then callback

resizer(base64Image).then((resizedBase64) => {
  console.log(resizedBase64);
  // the rest of your code goes here
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文