tinify / tinify-nodejs

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

Basic Promise Handling #27

Closed EvilJordan closed 3 years ago

EvilJordan commented 3 years ago

Given this simple function, how do I make tinify do its thing before "Complete!" is returned?

I've tried a multitude of different promise-wrapping methods, so I must be doing something simple incorrectly. I can't seem to get the return value of myFunction to happen only after tinify has finished its job.

myFunction = async (BUFFER, BUCKET, FILENAME) => {
    tinify.fromBuffer(BUFFER).toBuffer(async function(err, resultData) {
        await s3Manager.putObject(BUCKET, FILENAME, resultData);
    });
    return 'Complete!';
}
EvilJordan commented 3 years ago

I got help and got it working. Promises are so confusing.

await new Promise((resolve, reject) => {
    tinify.fromBuffer(BUFFER).toBuffer(async function(err, resultData) {
        await s3Manager.putObject(BUCKET, FILENAME, resultData);
        resolve();
    });
});