0

I have data passed to function where i need to check every object in array and return all promises , How can i pass same data to down in chaining so i can use for processing. Every promise is call to database to validate and insert data.

1- How to pass event to all promises for further processing ? 2- How to check if all events processing is done , then next func can invoke ?

Ctrl.js

       var data = [{
                activityId: '1tgktxy',
                variables: {
                    event: [Object]
                },
                priority: 0
            },
            {
                activityId: '7tl07',
                variables: {
                    caseIdFound: [{
                        type: Boolean,
                        value: true
                    }],
                    event: [Object]
                },
                priority: 0
            },
            {
                activityId: '7tl07',
                variables: {
                    caseIdFound: [{
                        type: Boolean,
                        value: true
                    }],
                    event: [Object]
                },
                priority: 0
            },
        ]

        function mainFunction(data) {
            return Promise.all(data.map(function(element) {
                    var event = JSON.parse(element.variables.event.value);
                    return checkTicketNumber(event);
                })
                .then(function() {
                    return insertDataToLog();
                }).
                .then(function() {
                    return insertDataToResult();
                })
                .then(function() {
                    console.log('All task events processing is done');
                   // anotherFunction();
                }).




            }

            var checkTicketNumber = new Promise(
                function(resolve, reject) {
                    checkTicketCall(ticketNumber, function(err, response) {
                        resolve();
                    }):
                });

           // if caseIdFound is true then i want invoke thise promise

            var insertDataToLog = new Promise(
                function(resolve, reject) {
                    InsertCall(event.body, function(err, response) {
                        resolve();
                    }):
                });

          // if caseIdFound is true then i want invoke thise promise
            var insertDatatoResult = new Promise(
                function(resolve, reject) {
                    InsertCall(event.body, function(err, response) {
                        resolve();
                    }):
                });

mainFunction(data);
hussain
  • 6,587
  • 18
  • 79
  • 152
  • Use it as a param (or property of a param) of `resolve` function. – raina77ow Aug 29 '17 at 19:48
  • 1
    What is `c`? What is `event`? Why does `mainFunction` never use the `data` it gets? Why are you calling promise objects as if they were functions? The code makes little sense as it stands. – trincot Aug 29 '17 at 19:48
  • sorry it suppose to be data instead of c.people. i corrected the code. – hussain Aug 29 '17 at 20:12
  • Why don't you ever use `person`? What is `element`? Did you actually debug this code before posting?? – trincot Aug 29 '17 at 20:14
  • did not notice this as well , its element is mapped object – hussain Aug 29 '17 at 20:16
  • In the other `new Promise` callbacks, what is `ticketNumber`, what is `event.body`, and why are `insertDataToLog` and `insertDatatoResult` defined in exactly the same way? (why need two then?). What is the point doing in `}).`? Where is the closing brace for `mainFunction`? Please .... you really should do some minimal quality check before posting. – trincot Aug 29 '17 at 20:34
  • i actually created this code to ask on SO actual code there is alot of unnecessary lines of code that is not needed for this question, if you can provide solution that will be helpful i been struggling last 3 hours with promises – hussain Aug 29 '17 at 20:52
  • i actually created this code to ask on SO actual code there is alot of unnecessary lines of code that is not needed for this question, if you can provide solution that will be helpful i been struggling last 3 hours with promises – hussain Aug 29 '17 at 20:53
  • the code in the question is completely unusable. For one thing, you are calling `checkTicketNumber()`, `insertDataToLog()` and `insertDataToResult()`... but none of these are a function, they are all Promises - so, the code wouldn't execute in the manner you assume - in fact, the code would fail – Jaromanda X Aug 29 '17 at 23:16
  • @JaromandaX i am trying to use promise so once one task is done it should move to next ,Can you please help here (example) to use correct approach for above task. – hussain Aug 30 '17 at 13:29

1 Answers1

0

You can ivoke resolve function with a parameter.

Example

var insertDataToLog = new Promise(
                function(resolve, reject) {
                    InsertCall(event.body, function(err, response) {
                        resolve({event: event, response: response});
                    }):
                });

Then you can get the resolved values;

...().then(function(resolveData) {
           console.log(resolveData); // resolveData is { event: event, response: response }
           return insertDataToLog();
      })
bennygenel
  • 23,896
  • 6
  • 65
  • 78