# Emitting event on close of other event (event chaining)

I have a software that does the following in sequence:

download text files from s3
collect the data from files


The program is asynchronous-synchronous - each phase is asynchronous, but it heavily depends on the output of the previous phase, so I'm using Promises for build the chaining.

The problem of this architecture is that is using to much CPU since I'm spawning child processes in the extract and collect phase. So I decide to change to events:

How I'm doing:

downloadfile.emit("copy", s3file);
extract.emit("file", file);
extract.on("error", callback);
extract.on("close", (file) => {
collect.emit("data", file.data);
collect.on("error", callback);
collect.on("close", (data) => {
data is here... i can use without waiting all the other files.
});
});
});


But this isn't very readable... I prefer the Promise way:

Promise.resolve(options)
.then(extract)
.then(collect);


So I was thinking... is it possible to chain the events? I mean, the output of an event will be the input of other, just like the Promises? If so, how could I achieve that?

• What does emit actually do here? Are downloadfile, extract and collect global entities, or per-sequence? – Joseph Mar 28 '17 at 14:17
• Per sequence, downloadfile -> extract -> collect – celoxxx Mar 28 '17 at 14:30

You should note that there's a significant difference between events and Promises. Events listen and fire multiple times. However, Promises are a one-time deal. Once it resolves/rejects, the callback won't fire again. You'll have to recreate the whole sequence for another series. With that:

is it possible to chain the events?

Yes and no. It depends on the event source. In your case, assuming downloadfile, extract and collect are event sources for a single operations, you can simply wrap that event source in a promise that resolves/rejects.

If so, how could i achieve that?

Assuming emit is a trigger to start the operation, error as the error listener and close as success, you can simply wrap them in promises, where error calls rejectand close calls resolve.

function downloadFiles(downloader, options){
return new Promise((resolve, reject) => {
});
}

function extract(extractor, file){
return new Promise((resolve, reject) => {
extractor.emit('copy', file);
extractor.on('error', reject);
extractor.on('close', resolve);
});
}

function collect(collector, data){
return new Promise((resolve, reject) => {
collector.emit('copy', data);
collector.on('error', reject);
collector.on('close', resolve);
});
}

function createSequence(){