-2
var job = new cronJob('* * * * * *', function () {
    Draft.find().then(data => {
        var finalData = data;
        finalData.forEach(function(item2) {
            if (item2.scheduledTime === 'now') {
                finalData.forEach(function (item) {
                    var psms = {
                        phoneno: item.senderdata,
                        sender: item.senderName,
                        message: item.message
                    }
                    var obj = psms;
                    var finalpostsms = obj.phoneno.split("\n").map(s => ({ ...obj,
                        phoneno: +s
                    }));
                    Profsms.bulkCreate(finalpostsms).then(function (data) {
                        if (data) {
                            console.log("successfully moved in profsms mysql");

                        } else {
                            console.log("failed");
                        }
                    })
                });
            } else {
                console.log('Better you be in drafts..manual input');
            }
            //delete from draft
            if (item2.scheduledTime === 'now') {
                Draft.findOneAndRemove({
                    _id: item2._id
                }, function (err, employee) {
                    if (err)
                        console.log('err');
                    console.log('Successfully deleted from draft');
                });
            } else {
                console.log('You cant delete from drafts hahaha because no sendnow statement');
            }
        });
    });
}, function () {
    console.log('DelCron Job finished.');
}, true, 'Asia/Calcutta');

This above code, working as asynchronously.

I want the above code to be work as synchronous, need some answers. I am a newbie for JS development

Is it possible to do with async await? i dont know how to write async await code.

Mohamed Sameer
  • 2,998
  • 3
  • 22
  • 51
  • 2
    You're looking for the documentation. – SLaks Dec 11 '17 at 15:01
  • willy you post answer? to work the above code as synchronous, it will be very useful for me :) – Mohamed Sameer Dec 11 '17 at 15:02
  • Possible duplicate of [How to wait for a JavaScript Promise to resolve before resuming function?](https://stackoverflow.com/questions/28921127/how-to-wait-for-a-javascript-promise-to-resolve-before-resuming-function) – Alex Blex Dec 11 '17 at 15:20
  • async/await doesn't magically turn your code to synchronous one. They still use promises behind the scene. – Alex Blex Dec 11 '17 at 15:23
  • What is synchronous and what is asynchronous? What is a Promise? Have you researched? Have you tried like 1 hour before asking here? I will answer you question, Yes you can do async/wait but you should try more. – Stamos Dec 11 '17 at 15:24
  • Yes i researched, but not working, i am new to JS, can you post answer? – Mohamed Sameer Dec 11 '17 at 15:27

2 Answers2

-1

Your callback should be

async function(){
   let data = await Draft.find();
   ...process data;
}
  • its showing syntax error near parameter "async function()" can you edit my question code , and post full answer it will be really helpful :) – Mohamed Sameer Dec 11 '17 at 15:06
  • @MohamedSameer: You need to run a version of Node that supports `async`. – SLaks Dec 11 '17 at 15:08
  • 1
    @MohamedSameer: We will not write your function for you. You need to learn how to write code. – SLaks Dec 11 '17 at 15:08
  • if once i got answer, i can know about this concept, then i can continue learning to write code, this i want to submit tomoro, thats why i am asking to edit my code, will be really helpful for me if you post answer – Mohamed Sameer Dec 11 '17 at 15:11
-1

This is an async await sample code, you can modify based on your need. But first you need to use Node that support async syntax. I believe node 8 LTS is already have async/await feature.

function get() {
 // your db code block
 return Promise.resolve(7);
}

async function main() {
  const r = await get();
  console.log(r);
}

main();
Adi Sembiring
  • 5,798
  • 12
  • 58
  • 70
  • what node version do you use ? have you try this sample code and run it in your terminal ? – Adi Sembiring Dec 11 '17 at 15:28
  • yes i have 8.9.1 its working, but my above code doesnot working – Mohamed Sameer Dec 11 '17 at 15:29
  • you need to explain more about what's not working. to simplify the check may be you need to test with simple code block ```async function processPromise() { const finalData = await Draft.find(); console.log('db result', finalData); // do another process console.log('process finished'); } new CronJob('* * * * * *', processPromise, null, true, 'America/Los_Angeles'); ``` – Adi Sembiring Dec 11 '17 at 16:08