In addition to Richard's comment about correctly using
async
/
await
in your JS, you're also using it in places where it isn't fully supported. For example, the JS array functions
forEach()
and
map()
don't return
Promise
objects.
let imgArray = await files.map((file) => {
let img = fs.readFileSync(file.path)
return encode_image = img.toString('base64')
})
...
let result = await imgArray.forEach(async(src, index) => {
...
})
Should be:
let imgArray = files.map(file => {
let img = fs.readFileSync(file.path)
return img.toString('base64');
})
...
for (const [index, src] of imgArray.entries()) {
...
}
Array.prototype.forEach() - JavaScript | MDN[
^] (documents that the return type is
undefined
)
Array.prototype.map() - JavaScript | MDN[
^] (documents that the return type is an array)