"use strict"

function callHandler (event, handler, next, reject) {
    setImmediate(() => {
        try {
            handler.fn(event, handler.state, handler.args, next);
        } catch (err) {
            reject(err);
        }
    });
}

module.exports = (args) => {

    const event = args[0];
    const sequence = args[1];
    event.output = event.input;

    return new Promise((resolve, reject) => {

        let handler = sequence[sequence.first];
        const next = (err, data, stream) => {

            if (err) {
                return reject(err);
            }

            event.data = data || event.data;
            event.output = stream || event.output;
            handler = sequence[handler.next];
            if (!handler) {
                return resolve(event);
            }

            // emit stream errors on event
            if (event.output.listenerCount('error') < 1) {
                event.output.on('error', (err) => event.emit('error', err));
            }

            callHandler(event, handler, next, reject);
        };

        callHandler(event, handler, next, reject);
    });
};

@@ -671,135 +671,9 @@
 put;
-%0A            handler = sequence%5Bhandler.next%5D;%0A            if (!handler) %7B%0A                return resolve(event);%0A            %7D
+ 
 %0A%0A  
@@ -856,32 +856,160 @@
 %0A            %7D%0A%0A
+            handler = sequence%5Bhandler.next%5D;%0A            if (!handler) %7B%0A                return resolve(event);%0A            %7D%0A%0A
             call

add default error event before resolve