javascript shows output of only last iteration of for loops with promise object inside

J. Doe

I wrote a node.js script to use jimp to get all image files from a directory, (which I take as input), run some manipulations on that and then save them in the target directory (another input) as filename.suffix.extension. I take suffix also as input.

But I only see the last file from the list that I collect as to be present in the target directory.

// imports
var Jimp = require('jimp');
const fs = require('fs')

// inputs
dir = process.argv[2]
target = process.argv[3]
suffix = process.argv[4]

// collect files
let dirCont = fs.readdirSync( dir );
const files = dirCont.filter( ( elm ) => /.*\.(png|jpg)/gi.test(elm) );

// run jimp on each file and write to target directory
for (file in files)
{  
    target_file = target+files[file].replace(/\.[^/.]+$/, "")+'.'+suffix+files[file].match(/\.[^/.]+$/)
    Jimp.read(dir+'/'+files[file]).then(function (file) {
        return file.resize(256, 256)     // resize
             .quality(60)                 // set JPEG quality
             .greyscale()                 // set greyscale
             .write(target_file); // save
    })

}

I run the entire thing using grunt.

following up from this question, things that I tried:

but still pretty much not working

Immediately-Invoked Function Expression

var Jimp = require('jimp');
const fs = require('fs')

dir = process.argv[2]
target = process.argv[3]
suffix = process.argv[4]
let dirCont = fs.readdirSync( dir );
const files = dirCont.filter( ( elm ) => /.*\.(png|jpg)/gi.test(elm) );

for (file in files)
{
    (function(index) {
    console.log("index: "+index)
    target_file = target+files[index].replace(/\.[^/.]+$/, "")+'.'+suffix+files[index].match(/\.[^/.]+$/)
    Jimp.read(dir+'/'+files[index]), function (err, filee) {
             if (err) throw err;
             filee.resize(256, 256)     // resize
             .quality(60)                 // set JPEG quality
             .greyscale()                 // set greyscale
             .write(target_file); // save
    }
    })(file);

}

Result:

Still only the last file is written

Function.prototype.bind

var Jimp = require('jimp');
const fs = require('fs')

dir = process.argv[2]
target = process.argv[3]
suffix = process.argv[4]
let dirCont = fs.readdirSync( dir );
const files = dirCont.filter( ( elm ) => /.*\.(png|jpg)/gi.test(elm) );
var funcs = {}
for (file in files)
{
    console.log(file)
    console.log(files[file])

    target_file = target+files[file].replace(/\.[^/.]+$/, "")+'.'+suffix+files[file].match(/\.[^/.]+$/)
    funcs[file] = Jimp.read(dir+'/'+files[file]), function (err, filee) {
        return filee.resize(256, 256)     // resize
             .quality(60)                 // set JPEG quality
             .greyscale()                 // set greyscale
             .write(target_file); // save
    }.bind(this, file)

}

for (var j = 0; j < 3; j++) {
  funcs[j]();
}

Result:

Error message:

  funcs[j]();
          ^
TypeError: funcs[j] is not a function

I discovered it is a promise object

forEach implementation

still only the last iteration was printed

Can anyone help me on this?

dave

The issue is that target_file, in each of these, is a shared variable (each iteration is modifying the same one). Just change:

target_file = 

to

let target_file = 

or

const target_file = 

and you should be fine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Gnuplot: How to "forget" last plot and replotting only things inside an iteration

From Dev

Function call inside loop taking only last iteration

From Dev

Saving loop output in a file only saves last iteration

From Dev

javascript Promise.all returns last promise only

From Dev

dealing with loops in javascript, only last item gets affected?

From Dev

Complexity of nested for loops starting from the last iteration

From Dev

Javascript - How to define Arrays inside an Object using Loops?

From Dev

Shows only the last item in the array

From Dev

For Loop only shows last item

From Dev

Report only shows the last record

From Dev

UICollectionView only shows the last cell

From Dev

Indexed Javascript Parse Promise Loops

From Dev

I am trying to display the number of occarances of a letter, but the output only shows the last letter

From Dev

Returning a promise inside promise (JavaScript)

From Dev

last iteration of forloop modify final output in vue

From Dev

Javascript slideshow shows no image for one iteration

From Dev

Only last iteration of while loop saves

From Dev

Python: list iteration only returns last value

From Dev

R nested loop returning only the last iteration

From Dev

While loop only finding the last iteration

From Dev

Python: list iteration only returns last value

From Dev

Java - reading .txt file to arrayList omits last iteration and shows error

From Dev

Java - reading .txt file to arrayList omits last iteration and shows error

From Dev

Javascript For loop loops through all the videos but only plays the last HTML5 video. Why?

From Dev

Javascript - AJAX request inside loops

From Dev

Javascript stored only the last one from a loop in an JSON Object

From Dev

SVG inline works but only shows <text> content when loaded inside <object>

From Dev

For loops in R iterates only the last entry

From Dev

Creating Plotly in R in loops shows the last chart for all elements

Related Related

  1. 1

    Gnuplot: How to "forget" last plot and replotting only things inside an iteration

  2. 2

    Function call inside loop taking only last iteration

  3. 3

    Saving loop output in a file only saves last iteration

  4. 4

    javascript Promise.all returns last promise only

  5. 5

    dealing with loops in javascript, only last item gets affected?

  6. 6

    Complexity of nested for loops starting from the last iteration

  7. 7

    Javascript - How to define Arrays inside an Object using Loops?

  8. 8

    Shows only the last item in the array

  9. 9

    For Loop only shows last item

  10. 10

    Report only shows the last record

  11. 11

    UICollectionView only shows the last cell

  12. 12

    Indexed Javascript Parse Promise Loops

  13. 13

    I am trying to display the number of occarances of a letter, but the output only shows the last letter

  14. 14

    Returning a promise inside promise (JavaScript)

  15. 15

    last iteration of forloop modify final output in vue

  16. 16

    Javascript slideshow shows no image for one iteration

  17. 17

    Only last iteration of while loop saves

  18. 18

    Python: list iteration only returns last value

  19. 19

    R nested loop returning only the last iteration

  20. 20

    While loop only finding the last iteration

  21. 21

    Python: list iteration only returns last value

  22. 22

    Java - reading .txt file to arrayList omits last iteration and shows error

  23. 23

    Java - reading .txt file to arrayList omits last iteration and shows error

  24. 24

    Javascript For loop loops through all the videos but only plays the last HTML5 video. Why?

  25. 25

    Javascript - AJAX request inside loops

  26. 26

    Javascript stored only the last one from a loop in an JSON Object

  27. 27

    SVG inline works but only shows <text> content when loaded inside <object>

  28. 28

    For loops in R iterates only the last entry

  29. 29

    Creating Plotly in R in loops shows the last chart for all elements

HotTag

Archive