javascriptnode.jswordpresswp-api

How to wait for image to download before executing the rest of the code


I hope you are doing okay, so I have been using nodejs for a while now, and I'm still getting used to asynchronous functions and stuff, so the thing is I download an image from a server using axios, and wait for image to download and then use the image to publish a post on a wordpress website, the code is Kinda tricky because it works couple of posts but others it just won't wait for the image to download completely, however it just share the post without the picture.

axios.get(encodeURI(img), {responseType: "stream"} )  
                                .then(response => {  
                                // Saving file to working directory  
                                    response.data.pipe(fs.createWriteStream("images/"+title.replace(/[^\x00-\x7F]/g, "")+".png"));
                                    await sleep(3000);
                                    var wp = new WPAPI({
                                        endpoint: 'XXXXX/wp-json',
                                        // This assumes you are using basic auth, as described further below
                                        username: 'XXXX',
                                        password: 'XXXXX'
                                    });
                                    wp.posts().create({
                                        title: title,
                                        content: index,
                                        categories: [3,9,2],
                                        status: 'publish'
                                      }).then(function( post ) {

                                        // Create the media record & upload your image file
                                        var filePath = "images/"+title.replace(/[^\x00-\x7F]/g, "")+".png";
                                        return wp.media().file( filePath ).create({
                                          title: title,
                                          // This property associates our new media record with our new post:
                                          post: post.id
                                        }).then(function( media ) {
                                          console.log( 'Media uploaded with ID #' + media.id );
                                          return wp.posts().id( post.id ).update({
                                            featured_media: media.id
                                          });                           
                                        });

                                      });
                                })  
                                    .catch(error => {  
                                    console.log(error);  
                                });  

So I'm asking how can I wait completely till the image exists on the folder before sharing the post thank you.


Solution

  • Instead of

    response.data.pipe(fs.createWriteStream("images/"+title.replace(/[^\x00-\x7F]/g, "")+".png"));
    
    await sleep(3000);
    

    Use

    response.data.pipe(fs.createWriteStream("images/"+title.replace(/[^\x00-\x7F]/g, "")+".png"))
     .on('error', () => {
        // log error and process 
      })
      .on('finish', () => {
        // publish a post with image on a wordpress website,
      });
    

    P.S. : try to make modular code,