'unexpected end of file' with pushing items to an array with jimp











up vote
0
down vote

favorite












i'm trying to overlay multiple images onto one single image. it works to an extent, but it isn't working how i want it to.



global.images = ;

fs.readdirSync('./images').forEach(function(file) {
images.push(file);

if (debug) {
console.log(chalk.green('[Debug] Pushed ' + chalk.blue(file) + ' to the array.'));
}
});
executeEdit(images);

function executeEdit(list) {
var jimps = ;
var x = 0;

for (var i = 0; i < images.length; i++) {
x = x + 150;

setTimeout((function(i) {
return function() {
jimps.push(jimp.read('images/' + images[i]));
}
})(i), 10 * x);
}


this code throws an error of:
(node:15352) UnhandledPromiseRejectionWarning: Error: unexpected end of file
at Inflate.zlibOnError (zlib.js:153:15)
at Inflate._processChunk (D:ImageTestnode_modulespngjslibsync-inflate.js:110:28)
at zlibBufferSync (D:ImageTestnode_modulespngjslibsync-inflate.js:151:17)
at inflateSync (D:ImageTestnode_modulespngjslibsync-inflate.js:155:10)
at module.exports (D:ImageTestnode_modulespngjslibparser-sync.js:79:20)
at Object.exports.read [as image/png] (D:ImageTestnode_modulespngjslibpng-sync.js:10:10)
at Jimp.parseBitmap (D:ImageTestnode_modules@jimpcoredistutilsimage-bitmap.js:117:53)
at Jimp.parseBitmap (D:ImageTestnode_modules@jimpcoredistindex.js:498:32)
at D:ImageTestnode_modules@jimpcoredistindex.js:440:15
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)
(node:15352) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)



and i have no idea why. it may be due to reading from the directory and adding it to an array as just doing var images = ['file.png', 'file2.png' ] works fine.



if anyone has any ideas on how to fix, please let me know.



thanks!



edit:
doing this works, but it only chooses the last image and not any others :/



for (var i = 0; i < images.length; i++) {
x = x + 150;
// console.log(i);

var image = {
_integer: i,
_image: images[i],
func: function() {
jimps.push(jimp.read('images/' + this._image));
// console.log(this._integer);
}
}

setTimeout(function() {image.func()}, x);
}









share|improve this question




























    up vote
    0
    down vote

    favorite












    i'm trying to overlay multiple images onto one single image. it works to an extent, but it isn't working how i want it to.



    global.images = ;

    fs.readdirSync('./images').forEach(function(file) {
    images.push(file);

    if (debug) {
    console.log(chalk.green('[Debug] Pushed ' + chalk.blue(file) + ' to the array.'));
    }
    });
    executeEdit(images);

    function executeEdit(list) {
    var jimps = ;
    var x = 0;

    for (var i = 0; i < images.length; i++) {
    x = x + 150;

    setTimeout((function(i) {
    return function() {
    jimps.push(jimp.read('images/' + images[i]));
    }
    })(i), 10 * x);
    }


    this code throws an error of:
    (node:15352) UnhandledPromiseRejectionWarning: Error: unexpected end of file
    at Inflate.zlibOnError (zlib.js:153:15)
    at Inflate._processChunk (D:ImageTestnode_modulespngjslibsync-inflate.js:110:28)
    at zlibBufferSync (D:ImageTestnode_modulespngjslibsync-inflate.js:151:17)
    at inflateSync (D:ImageTestnode_modulespngjslibsync-inflate.js:155:10)
    at module.exports (D:ImageTestnode_modulespngjslibparser-sync.js:79:20)
    at Object.exports.read [as image/png] (D:ImageTestnode_modulespngjslibpng-sync.js:10:10)
    at Jimp.parseBitmap (D:ImageTestnode_modules@jimpcoredistutilsimage-bitmap.js:117:53)
    at Jimp.parseBitmap (D:ImageTestnode_modules@jimpcoredistindex.js:498:32)
    at D:ImageTestnode_modules@jimpcoredistindex.js:440:15
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)
    (node:15352) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)



    and i have no idea why. it may be due to reading from the directory and adding it to an array as just doing var images = ['file.png', 'file2.png' ] works fine.



    if anyone has any ideas on how to fix, please let me know.



    thanks!



    edit:
    doing this works, but it only chooses the last image and not any others :/



    for (var i = 0; i < images.length; i++) {
    x = x + 150;
    // console.log(i);

    var image = {
    _integer: i,
    _image: images[i],
    func: function() {
    jimps.push(jimp.read('images/' + this._image));
    // console.log(this._integer);
    }
    }

    setTimeout(function() {image.func()}, x);
    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      i'm trying to overlay multiple images onto one single image. it works to an extent, but it isn't working how i want it to.



      global.images = ;

      fs.readdirSync('./images').forEach(function(file) {
      images.push(file);

      if (debug) {
      console.log(chalk.green('[Debug] Pushed ' + chalk.blue(file) + ' to the array.'));
      }
      });
      executeEdit(images);

      function executeEdit(list) {
      var jimps = ;
      var x = 0;

      for (var i = 0; i < images.length; i++) {
      x = x + 150;

      setTimeout((function(i) {
      return function() {
      jimps.push(jimp.read('images/' + images[i]));
      }
      })(i), 10 * x);
      }


      this code throws an error of:
      (node:15352) UnhandledPromiseRejectionWarning: Error: unexpected end of file
      at Inflate.zlibOnError (zlib.js:153:15)
      at Inflate._processChunk (D:ImageTestnode_modulespngjslibsync-inflate.js:110:28)
      at zlibBufferSync (D:ImageTestnode_modulespngjslibsync-inflate.js:151:17)
      at inflateSync (D:ImageTestnode_modulespngjslibsync-inflate.js:155:10)
      at module.exports (D:ImageTestnode_modulespngjslibparser-sync.js:79:20)
      at Object.exports.read [as image/png] (D:ImageTestnode_modulespngjslibpng-sync.js:10:10)
      at Jimp.parseBitmap (D:ImageTestnode_modules@jimpcoredistutilsimage-bitmap.js:117:53)
      at Jimp.parseBitmap (D:ImageTestnode_modules@jimpcoredistindex.js:498:32)
      at D:ImageTestnode_modules@jimpcoredistindex.js:440:15
      at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)
      (node:15352) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)



      and i have no idea why. it may be due to reading from the directory and adding it to an array as just doing var images = ['file.png', 'file2.png' ] works fine.



      if anyone has any ideas on how to fix, please let me know.



      thanks!



      edit:
      doing this works, but it only chooses the last image and not any others :/



      for (var i = 0; i < images.length; i++) {
      x = x + 150;
      // console.log(i);

      var image = {
      _integer: i,
      _image: images[i],
      func: function() {
      jimps.push(jimp.read('images/' + this._image));
      // console.log(this._integer);
      }
      }

      setTimeout(function() {image.func()}, x);
      }









      share|improve this question















      i'm trying to overlay multiple images onto one single image. it works to an extent, but it isn't working how i want it to.



      global.images = ;

      fs.readdirSync('./images').forEach(function(file) {
      images.push(file);

      if (debug) {
      console.log(chalk.green('[Debug] Pushed ' + chalk.blue(file) + ' to the array.'));
      }
      });
      executeEdit(images);

      function executeEdit(list) {
      var jimps = ;
      var x = 0;

      for (var i = 0; i < images.length; i++) {
      x = x + 150;

      setTimeout((function(i) {
      return function() {
      jimps.push(jimp.read('images/' + images[i]));
      }
      })(i), 10 * x);
      }


      this code throws an error of:
      (node:15352) UnhandledPromiseRejectionWarning: Error: unexpected end of file
      at Inflate.zlibOnError (zlib.js:153:15)
      at Inflate._processChunk (D:ImageTestnode_modulespngjslibsync-inflate.js:110:28)
      at zlibBufferSync (D:ImageTestnode_modulespngjslibsync-inflate.js:151:17)
      at inflateSync (D:ImageTestnode_modulespngjslibsync-inflate.js:155:10)
      at module.exports (D:ImageTestnode_modulespngjslibparser-sync.js:79:20)
      at Object.exports.read [as image/png] (D:ImageTestnode_modulespngjslibpng-sync.js:10:10)
      at Jimp.parseBitmap (D:ImageTestnode_modules@jimpcoredistutilsimage-bitmap.js:117:53)
      at Jimp.parseBitmap (D:ImageTestnode_modules@jimpcoredistindex.js:498:32)
      at D:ImageTestnode_modules@jimpcoredistindex.js:440:15
      at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)
      (node:15352) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)



      and i have no idea why. it may be due to reading from the directory and adding it to an array as just doing var images = ['file.png', 'file2.png' ] works fine.



      if anyone has any ideas on how to fix, please let me know.



      thanks!



      edit:
      doing this works, but it only chooses the last image and not any others :/



      for (var i = 0; i < images.length; i++) {
      x = x + 150;
      // console.log(i);

      var image = {
      _integer: i,
      _image: images[i],
      func: function() {
      jimps.push(jimp.read('images/' + this._image));
      // console.log(this._integer);
      }
      }

      setTimeout(function() {image.func()}, x);
      }






      node.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago

























      asked 2 days ago









      Joe Stevens

      105




      105
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          The hint is the message UnhandledPromiseRejectionWarning: you are missing the fact that jimp is actually asynchronous (either callbacks or promises). In your case, since there is no callback passed to jimp.read() it assumes promises are used but you are not using them correctly.



          If you replace // console.log(this._integer); by console.log(jimps), you will see something like this:



          [ Promise { <pending> } ]
          [ Promise { <Jimp 32x32> }, Promise { <pending> } ]
          ....


          I am not sure what you are doing with the jimps array at all (nor why you are using a setTimeout() here), but you can perhaps do something like this (if have simplified a bit by removing some logs and removing the setTimeout() stuff). This is now proper promise code.



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

          const images = ;
          fs.readdirSync('./images').forEach(function(file) {
          images.push(file);
          console.log('[Debug] Pushed', file, ' to the array');
          });

          executeEdit(images);
          console.log('waiting for executeEdit() to terminate');

          function executeEdit(list) {
          let jimp_read_promises =

          for (var i = 0; i < images.length; i++) {
          jimp_read_promises.push( jimp.read('images/' + images[i]))
          }

          Promise.all(jimp_read_promises).then( loadedimgs => {
          loadedimgs.map( img => {
          console.log( img )
          })
          })
          }


          So this would print the following



          [Debug] Pushed 7035a55d06033e435be112c0969b1820.png  to the array
          [Debug] Pushed aa5ef861d490a11fe20806e83c6dc64b.png to the array
          [Debug] Pushed crayon.png to the array
          waiting for executeEdit() to terminate
          <Jimp 32x32>
          <Jimp 48x48>
          <Jimp 256x256>


          So the 3 files (in my case) are read synchronously then executeEdit() is called. We build an array of promise objects then call Promise.all() which will wait till all promises are resolved. The value loadedimgs is an array with the result of each promise (in the same order as the promises). You can then loop over that array as you see fit (here I used a map call but a for loop will do.



          I hope this helps.






          share|improve this answer























          • thanks for the response! however, i am encountering problems when running this code. i still get the same error unexpected end of file and also the error await jimp.read('images/' + images[i]) ^^^^^ SyntaxError: await is only valid in async function. here is my current code: pastebin.com/raw/CsgPii9B - if you would have any idea on how to fix this, it would really help :^)
            – Joe Stevens
            2 days ago












          • could the unexpected end of file be due to one of your image being incorrect or corrupted ? Also can you tell me which version of node you are using ? The code I pasted was in a file called test.js which I ran with node test.js without issue. My version of node is v10.8.0. I will look at the pastebin.
            – tgo
            2 days ago










          • it might be to do with the version of node im using: im on v8.11.2 ://
            – Joe Stevens
            2 days ago










          • this is weird. I just installed v8.11.2 and run the file from pastebin unchanged. and I don't get any error message of any king neither releated to async nor to unexpected eof :( The output is here pastebin.com/raw/pJAkZ7zf (the undefined are normals as image[i] is not valid anymore by the time the promise is handled) Can you try with a few different images (incl. background) just to be sure ?
            – tgo
            2 days ago










          • ill quickly make a youtube video that showcases my issue gimme a min
            – Joe Stevens
            2 days ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239152%2funexpected-end-of-file-with-pushing-items-to-an-array-with-jimp%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          The hint is the message UnhandledPromiseRejectionWarning: you are missing the fact that jimp is actually asynchronous (either callbacks or promises). In your case, since there is no callback passed to jimp.read() it assumes promises are used but you are not using them correctly.



          If you replace // console.log(this._integer); by console.log(jimps), you will see something like this:



          [ Promise { <pending> } ]
          [ Promise { <Jimp 32x32> }, Promise { <pending> } ]
          ....


          I am not sure what you are doing with the jimps array at all (nor why you are using a setTimeout() here), but you can perhaps do something like this (if have simplified a bit by removing some logs and removing the setTimeout() stuff). This is now proper promise code.



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

          const images = ;
          fs.readdirSync('./images').forEach(function(file) {
          images.push(file);
          console.log('[Debug] Pushed', file, ' to the array');
          });

          executeEdit(images);
          console.log('waiting for executeEdit() to terminate');

          function executeEdit(list) {
          let jimp_read_promises =

          for (var i = 0; i < images.length; i++) {
          jimp_read_promises.push( jimp.read('images/' + images[i]))
          }

          Promise.all(jimp_read_promises).then( loadedimgs => {
          loadedimgs.map( img => {
          console.log( img )
          })
          })
          }


          So this would print the following



          [Debug] Pushed 7035a55d06033e435be112c0969b1820.png  to the array
          [Debug] Pushed aa5ef861d490a11fe20806e83c6dc64b.png to the array
          [Debug] Pushed crayon.png to the array
          waiting for executeEdit() to terminate
          <Jimp 32x32>
          <Jimp 48x48>
          <Jimp 256x256>


          So the 3 files (in my case) are read synchronously then executeEdit() is called. We build an array of promise objects then call Promise.all() which will wait till all promises are resolved. The value loadedimgs is an array with the result of each promise (in the same order as the promises). You can then loop over that array as you see fit (here I used a map call but a for loop will do.



          I hope this helps.






          share|improve this answer























          • thanks for the response! however, i am encountering problems when running this code. i still get the same error unexpected end of file and also the error await jimp.read('images/' + images[i]) ^^^^^ SyntaxError: await is only valid in async function. here is my current code: pastebin.com/raw/CsgPii9B - if you would have any idea on how to fix this, it would really help :^)
            – Joe Stevens
            2 days ago












          • could the unexpected end of file be due to one of your image being incorrect or corrupted ? Also can you tell me which version of node you are using ? The code I pasted was in a file called test.js which I ran with node test.js without issue. My version of node is v10.8.0. I will look at the pastebin.
            – tgo
            2 days ago










          • it might be to do with the version of node im using: im on v8.11.2 ://
            – Joe Stevens
            2 days ago










          • this is weird. I just installed v8.11.2 and run the file from pastebin unchanged. and I don't get any error message of any king neither releated to async nor to unexpected eof :( The output is here pastebin.com/raw/pJAkZ7zf (the undefined are normals as image[i] is not valid anymore by the time the promise is handled) Can you try with a few different images (incl. background) just to be sure ?
            – tgo
            2 days ago










          • ill quickly make a youtube video that showcases my issue gimme a min
            – Joe Stevens
            2 days ago















          up vote
          0
          down vote



          accepted










          The hint is the message UnhandledPromiseRejectionWarning: you are missing the fact that jimp is actually asynchronous (either callbacks or promises). In your case, since there is no callback passed to jimp.read() it assumes promises are used but you are not using them correctly.



          If you replace // console.log(this._integer); by console.log(jimps), you will see something like this:



          [ Promise { <pending> } ]
          [ Promise { <Jimp 32x32> }, Promise { <pending> } ]
          ....


          I am not sure what you are doing with the jimps array at all (nor why you are using a setTimeout() here), but you can perhaps do something like this (if have simplified a bit by removing some logs and removing the setTimeout() stuff). This is now proper promise code.



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

          const images = ;
          fs.readdirSync('./images').forEach(function(file) {
          images.push(file);
          console.log('[Debug] Pushed', file, ' to the array');
          });

          executeEdit(images);
          console.log('waiting for executeEdit() to terminate');

          function executeEdit(list) {
          let jimp_read_promises =

          for (var i = 0; i < images.length; i++) {
          jimp_read_promises.push( jimp.read('images/' + images[i]))
          }

          Promise.all(jimp_read_promises).then( loadedimgs => {
          loadedimgs.map( img => {
          console.log( img )
          })
          })
          }


          So this would print the following



          [Debug] Pushed 7035a55d06033e435be112c0969b1820.png  to the array
          [Debug] Pushed aa5ef861d490a11fe20806e83c6dc64b.png to the array
          [Debug] Pushed crayon.png to the array
          waiting for executeEdit() to terminate
          <Jimp 32x32>
          <Jimp 48x48>
          <Jimp 256x256>


          So the 3 files (in my case) are read synchronously then executeEdit() is called. We build an array of promise objects then call Promise.all() which will wait till all promises are resolved. The value loadedimgs is an array with the result of each promise (in the same order as the promises). You can then loop over that array as you see fit (here I used a map call but a for loop will do.



          I hope this helps.






          share|improve this answer























          • thanks for the response! however, i am encountering problems when running this code. i still get the same error unexpected end of file and also the error await jimp.read('images/' + images[i]) ^^^^^ SyntaxError: await is only valid in async function. here is my current code: pastebin.com/raw/CsgPii9B - if you would have any idea on how to fix this, it would really help :^)
            – Joe Stevens
            2 days ago












          • could the unexpected end of file be due to one of your image being incorrect or corrupted ? Also can you tell me which version of node you are using ? The code I pasted was in a file called test.js which I ran with node test.js without issue. My version of node is v10.8.0. I will look at the pastebin.
            – tgo
            2 days ago










          • it might be to do with the version of node im using: im on v8.11.2 ://
            – Joe Stevens
            2 days ago










          • this is weird. I just installed v8.11.2 and run the file from pastebin unchanged. and I don't get any error message of any king neither releated to async nor to unexpected eof :( The output is here pastebin.com/raw/pJAkZ7zf (the undefined are normals as image[i] is not valid anymore by the time the promise is handled) Can you try with a few different images (incl. background) just to be sure ?
            – tgo
            2 days ago










          • ill quickly make a youtube video that showcases my issue gimme a min
            – Joe Stevens
            2 days ago













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          The hint is the message UnhandledPromiseRejectionWarning: you are missing the fact that jimp is actually asynchronous (either callbacks or promises). In your case, since there is no callback passed to jimp.read() it assumes promises are used but you are not using them correctly.



          If you replace // console.log(this._integer); by console.log(jimps), you will see something like this:



          [ Promise { <pending> } ]
          [ Promise { <Jimp 32x32> }, Promise { <pending> } ]
          ....


          I am not sure what you are doing with the jimps array at all (nor why you are using a setTimeout() here), but you can perhaps do something like this (if have simplified a bit by removing some logs and removing the setTimeout() stuff). This is now proper promise code.



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

          const images = ;
          fs.readdirSync('./images').forEach(function(file) {
          images.push(file);
          console.log('[Debug] Pushed', file, ' to the array');
          });

          executeEdit(images);
          console.log('waiting for executeEdit() to terminate');

          function executeEdit(list) {
          let jimp_read_promises =

          for (var i = 0; i < images.length; i++) {
          jimp_read_promises.push( jimp.read('images/' + images[i]))
          }

          Promise.all(jimp_read_promises).then( loadedimgs => {
          loadedimgs.map( img => {
          console.log( img )
          })
          })
          }


          So this would print the following



          [Debug] Pushed 7035a55d06033e435be112c0969b1820.png  to the array
          [Debug] Pushed aa5ef861d490a11fe20806e83c6dc64b.png to the array
          [Debug] Pushed crayon.png to the array
          waiting for executeEdit() to terminate
          <Jimp 32x32>
          <Jimp 48x48>
          <Jimp 256x256>


          So the 3 files (in my case) are read synchronously then executeEdit() is called. We build an array of promise objects then call Promise.all() which will wait till all promises are resolved. The value loadedimgs is an array with the result of each promise (in the same order as the promises). You can then loop over that array as you see fit (here I used a map call but a for loop will do.



          I hope this helps.






          share|improve this answer














          The hint is the message UnhandledPromiseRejectionWarning: you are missing the fact that jimp is actually asynchronous (either callbacks or promises). In your case, since there is no callback passed to jimp.read() it assumes promises are used but you are not using them correctly.



          If you replace // console.log(this._integer); by console.log(jimps), you will see something like this:



          [ Promise { <pending> } ]
          [ Promise { <Jimp 32x32> }, Promise { <pending> } ]
          ....


          I am not sure what you are doing with the jimps array at all (nor why you are using a setTimeout() here), but you can perhaps do something like this (if have simplified a bit by removing some logs and removing the setTimeout() stuff). This is now proper promise code.



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

          const images = ;
          fs.readdirSync('./images').forEach(function(file) {
          images.push(file);
          console.log('[Debug] Pushed', file, ' to the array');
          });

          executeEdit(images);
          console.log('waiting for executeEdit() to terminate');

          function executeEdit(list) {
          let jimp_read_promises =

          for (var i = 0; i < images.length; i++) {
          jimp_read_promises.push( jimp.read('images/' + images[i]))
          }

          Promise.all(jimp_read_promises).then( loadedimgs => {
          loadedimgs.map( img => {
          console.log( img )
          })
          })
          }


          So this would print the following



          [Debug] Pushed 7035a55d06033e435be112c0969b1820.png  to the array
          [Debug] Pushed aa5ef861d490a11fe20806e83c6dc64b.png to the array
          [Debug] Pushed crayon.png to the array
          waiting for executeEdit() to terminate
          <Jimp 32x32>
          <Jimp 48x48>
          <Jimp 256x256>


          So the 3 files (in my case) are read synchronously then executeEdit() is called. We build an array of promise objects then call Promise.all() which will wait till all promises are resolved. The value loadedimgs is an array with the result of each promise (in the same order as the promises). You can then loop over that array as you see fit (here I used a map call but a for loop will do.



          I hope this helps.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          tgo

          1,185511




          1,185511












          • thanks for the response! however, i am encountering problems when running this code. i still get the same error unexpected end of file and also the error await jimp.read('images/' + images[i]) ^^^^^ SyntaxError: await is only valid in async function. here is my current code: pastebin.com/raw/CsgPii9B - if you would have any idea on how to fix this, it would really help :^)
            – Joe Stevens
            2 days ago












          • could the unexpected end of file be due to one of your image being incorrect or corrupted ? Also can you tell me which version of node you are using ? The code I pasted was in a file called test.js which I ran with node test.js without issue. My version of node is v10.8.0. I will look at the pastebin.
            – tgo
            2 days ago










          • it might be to do with the version of node im using: im on v8.11.2 ://
            – Joe Stevens
            2 days ago










          • this is weird. I just installed v8.11.2 and run the file from pastebin unchanged. and I don't get any error message of any king neither releated to async nor to unexpected eof :( The output is here pastebin.com/raw/pJAkZ7zf (the undefined are normals as image[i] is not valid anymore by the time the promise is handled) Can you try with a few different images (incl. background) just to be sure ?
            – tgo
            2 days ago










          • ill quickly make a youtube video that showcases my issue gimme a min
            – Joe Stevens
            2 days ago


















          • thanks for the response! however, i am encountering problems when running this code. i still get the same error unexpected end of file and also the error await jimp.read('images/' + images[i]) ^^^^^ SyntaxError: await is only valid in async function. here is my current code: pastebin.com/raw/CsgPii9B - if you would have any idea on how to fix this, it would really help :^)
            – Joe Stevens
            2 days ago












          • could the unexpected end of file be due to one of your image being incorrect or corrupted ? Also can you tell me which version of node you are using ? The code I pasted was in a file called test.js which I ran with node test.js without issue. My version of node is v10.8.0. I will look at the pastebin.
            – tgo
            2 days ago










          • it might be to do with the version of node im using: im on v8.11.2 ://
            – Joe Stevens
            2 days ago










          • this is weird. I just installed v8.11.2 and run the file from pastebin unchanged. and I don't get any error message of any king neither releated to async nor to unexpected eof :( The output is here pastebin.com/raw/pJAkZ7zf (the undefined are normals as image[i] is not valid anymore by the time the promise is handled) Can you try with a few different images (incl. background) just to be sure ?
            – tgo
            2 days ago










          • ill quickly make a youtube video that showcases my issue gimme a min
            – Joe Stevens
            2 days ago
















          thanks for the response! however, i am encountering problems when running this code. i still get the same error unexpected end of file and also the error await jimp.read('images/' + images[i]) ^^^^^ SyntaxError: await is only valid in async function. here is my current code: pastebin.com/raw/CsgPii9B - if you would have any idea on how to fix this, it would really help :^)
          – Joe Stevens
          2 days ago






          thanks for the response! however, i am encountering problems when running this code. i still get the same error unexpected end of file and also the error await jimp.read('images/' + images[i]) ^^^^^ SyntaxError: await is only valid in async function. here is my current code: pastebin.com/raw/CsgPii9B - if you would have any idea on how to fix this, it would really help :^)
          – Joe Stevens
          2 days ago














          could the unexpected end of file be due to one of your image being incorrect or corrupted ? Also can you tell me which version of node you are using ? The code I pasted was in a file called test.js which I ran with node test.js without issue. My version of node is v10.8.0. I will look at the pastebin.
          – tgo
          2 days ago




          could the unexpected end of file be due to one of your image being incorrect or corrupted ? Also can you tell me which version of node you are using ? The code I pasted was in a file called test.js which I ran with node test.js without issue. My version of node is v10.8.0. I will look at the pastebin.
          – tgo
          2 days ago












          it might be to do with the version of node im using: im on v8.11.2 ://
          – Joe Stevens
          2 days ago




          it might be to do with the version of node im using: im on v8.11.2 ://
          – Joe Stevens
          2 days ago












          this is weird. I just installed v8.11.2 and run the file from pastebin unchanged. and I don't get any error message of any king neither releated to async nor to unexpected eof :( The output is here pastebin.com/raw/pJAkZ7zf (the undefined are normals as image[i] is not valid anymore by the time the promise is handled) Can you try with a few different images (incl. background) just to be sure ?
          – tgo
          2 days ago




          this is weird. I just installed v8.11.2 and run the file from pastebin unchanged. and I don't get any error message of any king neither releated to async nor to unexpected eof :( The output is here pastebin.com/raw/pJAkZ7zf (the undefined are normals as image[i] is not valid anymore by the time the promise is handled) Can you try with a few different images (incl. background) just to be sure ?
          – tgo
          2 days ago












          ill quickly make a youtube video that showcases my issue gimme a min
          – Joe Stevens
          2 days ago




          ill quickly make a youtube video that showcases my issue gimme a min
          – Joe Stevens
          2 days ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239152%2funexpected-end-of-file-with-pushing-items-to-an-array-with-jimp%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine