How can I return a value from two asynchronous method?

I am trying to create an image compressor without any third-party library.

Here is my code(it will finally returns the blob of compressed image):

 async function CompressImage(File, Quality, FileType) {
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext('2d');
        var img = new Image;
        img.onload = function () {
            canvas.width = img.width;
            canvas.height = img.height;
            ctx.drawImage(img, 0, 0, img.width, img.height);                        
                canvas.toBlob(function (blob) {                    
                    return blob;
                }, FileType, Quality);            
        img.src = URL.createObjectURL(File);

It seems there is nothing wrong with my code while always returns an undefined value.

I found out maybe it is the asynchronous problem. The img.onload and the canvas.toBlob are asynchronous methods.

I think the promise may help this while I don’t know how to order these methods run one by one and finally returns a value.

How can I solve this? Thank you.

174 thoughts on “How can I return a value from two asynchronous method?”

  1. Hey would you mind letting me know which webhost
    you’re working with? I’ve loaded your blog in 3 completely different browsers and
    I must say this blog loads a lot faster then most.
    Can you recommend a good hosting provider at
    a honest price? Thanks, I appreciate it!

  2. I was curious if you ever thought of changing the page layout of your website?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having one
    or 2 pictures. Maybe you could space it out better?

  3. I was suggested this web site by my cousin. I’m not positive whether or not
    this post is written through him as no one else know such certain about my trouble.
    You are amazing! Thank you!

  4. My partner and I stumbled over here coming from a different
    web address and thought I may as well check things out.
    I like what I see so i am just following you. Look forward to going over your web page again.

  5. I’m amazed, I must say. Seldom do I encounter
    a blog that’s equally educative and interesting,
    and without a doubt, you’ve hit the nail on the head.
    The problem is something that not enough people are speaking intelligently
    about. Now i’m very happy I came across this
    in my search for something concerning this.

  6. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that
    automatically tweet my newest twitter updates. I’ve been looking for
    a plug-in like this for quite some time and was hoping maybe you would
    have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new

  7. It’s a shame you don’t have a donate button!
    I’d definitely donate to this excellent blog! I guess for now i’ll settle for bookmarking and adding your RSS feed
    to my Google account. I look forward to new updates
    and will talk about this site with my Facebook group.
    Talk soon! quest bars quest bars

  8. Have you ever considered about including a little bit more
    than just your articles? I mean, what you say is fundamental and all.
    But think about if you added some great photos or video clips to give your posts more, “pop”!
    Your content is excellent but with images and clips, this site could certainly be one of the very best in its field.
    Awesome blog! scoliosis surgery scoliosis surgery