question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Awaiting multiple Jimp page creation before running final function

See original GitHub issue

I have two images that I’m adding text to, and then after that I have a function that converts those images to PDF format. I’ve tested the PDF generator, and that seems to be fine.

I attempted to emulate the syntax shown in #463 while editing each file. I load a Promise.all with the file and font, and then make my changes. However, I’m getting the following error:

TypeError: image.print is not a function

Two questions: Why am I getting the type error? What’s the best way to fix? What’s the proper way to pass all the functions in, await for them to completed, and then advance to final step?

exports.generate = (params) => {
    // Params: id, 
    // 
    checkDirectory('./cache/' + params.order_id)
    return Promise.all([
        Jimp.read("./template_docs/file1.png"), 
        Jimp.loadFont(Jimp.FONT_SANS_32_BLACK)
    ]).then((image, font) => {
        image.print(font, 628, 205, "" + params.first_value); 
        // phone
        image.print(font, 830, 260, "" + params.second_value);

        image.write('./cache/' + params.order_id + "/send1.png")
    })
    .then(function(){
        return Promise.all([
            Jimp.read("./template_docs/file2.png"), 
            Jimp.loadFont(Jimp.FONT_SANS_32_BLACK)
        ]).then((image, font) => {
                // date
            image.print(font, 480, 200, getDate());

            return image.write('./cache/' + params.order_id + "/send2.png")
        })
    }).then(function() {
        setTimeout(convertImagesToPDF({"input": [
                "./cache/" + params.order_id + "/send1.png", 
                "./cache/" + params.order_id + "/send2.png"
            ], "output": "./cache/" + params.order_id + "/to_sign.pdf"
        }), 10000);
        
    }).catch(err => {
        console.log(err)
    })
}

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
hipstersmoothiecommented, Jul 27, 2018

when #486 merges it will be a lot easier since you can have write return promises but for now

Jimp.loadFont(Jimp.FONT_SANS_32_BLACK)
  .then(font => {
    return Jimp.read('./template_docs/file1.png')
      .then(image => {
        image.print(font, 628, 205, '' + params.first_value);
        image.print(font, 830, 260, '' + params.second_value);

        return new Promise((resolve, reject) =>
          image.write(
            './cache/' + params.order_id + '/send1.png',
            (err, res) => {
              if (err) reject(err);
              else resolve(res);
            }
          )
        );
      })
      .then(Jimp.read('./template_docs/file2.png'))
      .then(image2 => {
        image2.print(font, 480, 200, getDate());
        image2.write('./cache/' + params.order_id + '/send2.png');

        return new Promise((resolve, reject) =>
          image2.write(
            './cache/' + params.order_id + '/send2.png',
            (err, res) => {
              if (err) reject(err);
              else resolve(res);
            }
          )
        );
      });
  })
  .then(() => {
    convertImagesToPDF({
      input: [
        './cache/' + params.order_id + '/send1.png',
        './cache/' + params.order_id + '/send2.png'
      ],
      output: './cache/' + params.order_id + '/to_sign.pdf'
    });
    console.log('Completed')
  });
1reaction
awcchungstercommented, Jul 27, 2018

@hipstersmoothie Thank you for all your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

In JavaScript, does using await inside a loop block the loop?
The await keyword means to wait until this value (or error) has been finalized. So from the perspective of the running function, it...
Read more >
Multiprocessing Pool Wait For All Tasks To Finish in Python
You can wait for tasks issued to the multiprocessing pool to complete by calling AsyncResult.wait() or calling Pool.join().
Read more >
JavaScript — from callbacks to async/await - freeCodeCamp
Before the code executes, var and function declarations are “hoisted” to the top of their scope. This is an example of a synchronous...
Read more >
How JavaScript works: Event loop and the rise of Async ...
A simple way of “waiting” for an asynchronous function to return its result is to use a function called callback: Just a note:...
Read more >
Concurrency and async / await - FastAPI
You can only use await inside of functions created with async def . ... of parallelism and multiprocessing (having multiple processes running in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found