tinify / tinify-nodejs

Node.js client for the Tinify API.
https://tinypng.com/developers
MIT License
421 stars 74 forks source link

Cannot get quantity after performing a request compression #46

Closed shenxxu720 closed 7 months ago

shenxxu720 commented 9 months ago

After I perform compression (tinify.fromFile()), the value of tinify.compressionCount is undefined. Can I get the quantity only in the callback of tinify.validate()?

PabloPerezDeCiriza commented 9 months ago

Hi shenxxu720,

The issue you are finding is because tinify.fromFile() is an asyncronous operation. The output is undefined because tinify.compressionCount is being executed before tinify.fromFile() is completed. You can try something like this:

const tinify = require("tinify");
tinify.key = "YOUR_API_KEY";

tinify.fromFile("panda.png").toFile("panda_compressed.png").then(() => {
    console.log("Compression count:", tinify.compressionCount);
});