Node.js中使用Sharp处理图片时,保存Promise返回值到变量显示pending状态的问题求助
Hey there! Let's break down why you're seeing that Promise { pending } message and how to fix it so you can use your resized Base64 string in subsequent code.
Why This Happens
Your resizer function is marked as async, which means it always returns a Promise—even if you return a raw value, it gets wrapped in a resolved Promise. When you assign resizer(base64Image) directly to resizedBase64, the asynchronous image processing hasn't finished yet, so the Promise is still in the "pending" state. The .then() callback works because it only runs once the Promise resolves (i.e., when Sharp finishes processing the image).
Solution 1: Use async/await for Clean, Synchronous-Looking Code
Since await can only be used inside an async function, wrap your function call in an async wrapper to get the resolved value directly:
// Move imports to the top (best practice in Node.js) const { Buffer } = require("buffer"); const sharp = require("sharp"); async function resizer(base64, width = 224, height = 224) { if (!base64) { // Throw a proper Error object instead of console.error throw new Error("not a base64 string"); } const base64Buffer = Buffer.from(base64, "base64"); const resizedBuffer = await sharp(base64Buffer) .resize({ width, height, fit: sharp.fit.cover, }) .rotate(90) .toBuffer(); return resizedBuffer.toString("base64"); } // Wrap your call in an async function to use await async function processResizedImage() { try { const resizedBase64 = await resizer(base64Image); console.log(resizedBase64); // ✅ Use your resized Base64 string here for subsequent logic // Example: uploadToCloudStorage(resizedBase64); } catch (err) { console.error("Error processing image:", err.message); } } processResizedImage();
Solution 2: Keep Using .then() and Chain Your Logic
If you prefer not to add an extra async function, move all code that depends on the resized Base64 string inside the .then() callback. This ensures it only runs after the Promise resolves:
const { Buffer } = require("buffer"); const sharp = require("sharp"); async function resizer(base64, width = 224, height = 224) { if (!base64) { throw new Error("not a base64 string"); } const base64Buffer = Buffer.from(base64, "base64"); const resizedBuffer = await sharp(base64Buffer) .resize({ width, height, fit: sharp.fit.cover, }) .rotate(90) .toBuffer(); return resizedBuffer.toString("base64"); } resizer(base64Image) .then((resizedBase64) => { console.log(resizedBase64); // ✅ Put all code that needs resizedBase64 here // Example: saveToDatabase(resizedBase64); }) .catch((err) => { console.error("Error processing image:", err.message); });
Quick Code Improvements
- Move imports to the top: Don't require modules inside functions—this avoids redundant module loading and follows Node.js conventions.
- Proper error handling: Replace
throw console.error(...)withthrow new Error(...)so you can catch and handle errors properly intry/catchor.catch(). - Clear variable names: Renamed
imagetoresizedBufferto make its purpose more obvious.
内容的提问来源于stack exchange,提问作者Kwinten

