0

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 !!

Ved sinha
  • 29
  • 1
  • 8
  • You seem to call functions with a different number of arguments than there are function parameters. Secondly, if you are going for promises, then you'll get more orderly code if you first create promisified alternatives for each callback-driven function that you use. – trincot Sep 30 '20 at 14:27
  • Can you change the code using promises ? – Ved sinha Sep 30 '20 at 14:32
  • For that you would need to complete your question with code that can be run, that actually calls one of those functions, and with expected output (for certain input). – trincot Sep 30 '20 at 14:36

0 Answers0