Home > Blockchain >  When trying to write image file it moves ahead and write works late
When trying to write image file it moves ahead and write works late

Time:03-11

What i am trying to do here is i am writing a image file after resizing it with node and after that i will use it to upload it. but when i am writing it my uploading code started at that time and file is still not written.

async function getdataFromJimpSmall(image,filePath,FILE_NAME){
try{
   var isSucess = false ;
   const im = await Jimp.read(image);
    return new Promise((resolve, reject) => {
      try{
        im.resize(150, 215, Jimp.RESIZE_BEZIER, function(err){
          if (err) throw err;
        });
        im.write(filePath FILE_NAME)
        resolve(true)
        
      }
      catch(err){
        console.log(err);
        reject(false);
      }
    })

}
    catch(err){
        console.log('getdataFromJimpSmall Err====>' err)
        return false
    }
  

}

I tried to use this but it is getting ahead of my code it started from here and it is getting called from here.

isThumbnailUrlavailable = await jimpController.imageCoverJimpAndUploadToS3small(getISBNBook, "[email protected]", "SaleIsbn")
        isThumbnailUrlavailableMedium = await jimpController.imageCoverJimpAndUploadToS3medium(getISBNBook, "[email protected]", "SaleIsbn")

And the first function imageCoverJimpAndUploadToS3small() is this:

exports.imageCoverJimpAndUploadToS3small = async (getContent,email,file) =>{
try{
    var elementType = 15;
    var filePath = path.join(__dirname, '../../uploads/t/')
            var FILE_NAME = `${getContent.contentMetaDataRef.isbn}_s.jpg`
            var image =  getContent.thumbnail
            let tempdata = await getdataFromJimpSmall(image,filePath,FILE_NAME)
            console.log('temp - ', tempdata);
          if(tempdata==true)
          {
            var data = await commonController.uploadthumbnailTOPublicS3(filePath, FILE_NAME)
            console.log('s3', data);
              requestImageSize(data.url).then(size => 
                {
                  console.log(size);
                  if(size.height>0&&size.width>0)
                  {
                    tempdata=true;
                    const getContentElement =  ContentElement.findOne({contentId: getContent._id,elementType,elementData: data.keyName}).lean()
                    if(getContentElement){
                         ContentElement.findByIdAndUpdate(getContentElement._id, {
                            createdOn: Date(), modifiedOn: Date(),
                        }, { new: true })
                    }
                    else
                    {
                    
                     return tempdata;
                  }else
                  {
                    tempdata=false;
                    return tempdata;
                  }
                 
                }).catch(err => 
                  {
                    console.error(err);
                    tempdata=false;
                    return tempdata;
                  });
          }
}
catch(error){
    console.log(error)
    tempdata=false;
    return tempdata;
}

}

But it is not working...

Again i am calling imageCoverJimpAndUploadToS3small() and it starts after that.

CodePudding user response:

as per docs https://www.npmjs.com/package/jimp#writing-to-files-and-buffers, .write runs asynchronously and takes a callback, which is why the promise resolves before image is written

so, you need to use callback, and resolve from there:

im.write(filePath FILE_NAME, (err)=>{
    if(err) throw err;
    resolve(true);
});

or, as the docs say, you might use and await .writeAsync, which is promise-based:

await im.writeAsync(filePath FILE_NAME);
resolve(true);
  • Related