0

I´m new in Node.js and just learn the basics in using Promises and async functions. Currently I´m struggling to achieve a return from a nested function using Promises. Although there are similar questions to (I think the same issue), I still don´t get it working. Here´s what I want to achieve.

With the function jobScheduler I create a new job using the node-schedule module which is retrieving data from a service between two dates (variables start and end) with an specific recurrence rule (variable rule). The service is returning data in form of true / false (which is working properly). And now I need to return this value of the service by the jobScheduler function with a promise. The job scheduler is working perfectly as it should, I´m just struggeling to return the true / false value by my jobScheduler function.

function jobScheduler(start, end, rule, cycle) {
    return new Promise(function (resolve, reject) {
        schedule.scheduleJob({start: start, end: end, rule: rule}, async  function () {
            const data = await service.getData(cycle);
            resolve(data);
        });
    });
}
stefano
  • 315
  • 2
  • 16
  • Asynchronously derived values can't be returned. `jobScheduler()` is correctly written to return `Promise`; the Promise will *deliver* `true/false`. The calling function must be written to access the delivered value via `Promise.then(...)` or the equivalent `async/await` syntax. It's a common misunderstanding that asynchronously derived values should somehow be returned. I have voted to close with a reference to a comprehensively answered question. – Roamer-1888 Dec 23 '19 at 05:43
  • Thank you very much for clearing up. I´m calling this function inside a async one together with await. Merry christmas! – stefano Dec 24 '19 at 09:23

0 Answers0