How to ensure that the next steps are performed after all the callback functions of the array forEach have been executed

The

background is file traversal and operations are performed on each file
traversing through Synchronize to get the path of each file, and saving it in an array
array forEach input functions, using the module node-exiftool to read the information of each file (mainly pictures), which creates a bunch of callback functions. Constantly chaining . Then
node-exiftool requires . Then (() = > ep.close ()) to end the program
how to determine that all callback functions have been completed?

example given by node-exiftool

const exiftool = require("node-exiftool")
const ep = new exiftool.ExiftoolProcess()

ep
  .open()
  // display pid
  .then((pid) => console.log("Started exiftool process %s", pid))
  .then(() => ep.readMetadata("photo.jpg", ["-File:all"]))
  .then(console.log, console.error)
  .then(() => ep.readMetadata("photo2.jpg", ["-File:all"]))
  .then(console.log, console.error)
  .then(() => ep.close())
  .then(() => console.log("Closed exiftool"))
  .catch(console.error)

as a module, I divided the process into three steps: open, read, and close

const exiftool = require("node-exiftool")
const exiftoolBin = require("dist-exiftool")
const ep = new exiftool.ExiftoolProcess(exiftoolBin)
exports.openExif = () => ep.open()
exports.getDate = (openedEp, path, cb) => {
    return openedEp
        .then(() => ep.readMetadata(path, ["charset filename=utf8", "CreateDate"]))
        .then(cb, console.error)
}
exports.closeExif = finishedEp => finishedEp.then(ep.close()).catch(console.error)

then another file calls the module

let openedEP = exif.openExif()
fileArr.forEach((item) => {
    openedEp = exif.getDate(openedEP, item, handleDate)
    function handleDate(metadata) {
        let date = parse(metadata.data[0].CreateDate)
        console.log(path.basename(item) + ": " + date.toJSON())
    }
})

output of the first two lines of the console

(node:11208) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 finish listeners added. Use emitter.setMaxListeners() to increase limit
(node:11208) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 finish listeners added. Use emitter.setMaxListeners() to increase limit

I don"t know why
and the program won"t quit

beginners, ask for experienced answers

Mar.28,2021

solve it by yourself, or lack of knowledge of promise
pack all the promise, and finally wrap it in Promise.all

Menu