How to handle callbacks?

42 views Asked by At

I have a function which is -:

function generateAndEmitSessionizedEvents(workflowIdVsClientId, workflowIdVsEvents, clientVsIdVsDoc, esWriteFailedWorkflowIds) {
    return _generateSessionizedEvents(workflowIdVsClientId, workflowIdVsEvents, clientVsIdVsDoc, esWriteFailedWorkflowIds)
        .then(function (messageInfo) {
            return new Promise((resolve, reject) => {
                preValidateUtil.preValidateNullCheck(messageInfo, "vo",  function (messageInfo, err) {
                    if(err) {
                        logger.error("Failed to send slack notification" , err);
                    }
                    return resolve(messageInfo);
                });
            })
    } 

I have passed inside the promise preValidateNullCheck function which is -:

function notifySlack(faultyEventsList, callback) {
    var text = `The data for faulty event is : ${faultyEventsList}`;
    slackHelper.slack.sendNotification(text, function (err) {
        if (err) {
            logger.error("ERROR Failed to send the slack notification" + err);
            return callback(err);
        }
    })
}


function preValidateNullCheck(messagesInfo, stream, callback) {
    var faultyEventsList = [];
    var validEventsList = [];
    _.each(messagesInfo, function (messageInfo) {
        var event = messageInfo.message.event_type;
        findSchemaForEvent(event, messageInfo, stream, faultyEventsList, validEventsList);
    });
    notifySlack(faultyEventsList);
    return validEventsList;
}

In notifyslack function i have handled the error and still have to handle the success callback, can't understand how to do it.

Please Note -: preValidateNullCheck function is not correct, I don't know weather i should have passed callback here too in arguments or in notifySlack function.

I can't understand how to handle callbacks as in, notifySlack must occur after everything happens in preValidateNullCheck function.

I am new to this, seeking help !!

0

There are 0 answers