question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generator style callback does not work

See original GitHub issue

Hi, I am new to ES6 and node-schedule. I am trying to schedule a job with a generator function as callback. I saw that #210 mentioned that generator was supported. Here is my code below:

const date = momemt().add(1, 's'); // 1 second after now.
schedule.scheduleJob('taksid', date, function*(){
    console.log('Hi');
    const result = yield Message.findAll();
    console.log('Result: ' + result);
});

The Hi will be printed out correctly, but the Result will never printed out. I am using yield Message.findAll() quite a lot in other places without problem, so I guess it might be something in the generator way of node-schedule.

I also tried to insert an yield line in the test cases of node-schedule, right before the test.ok(true);:

var job = new schedule.Job(function*() {
    yield 1; // Insert this yield statement
    test.ok(true);
});

The test becomes failing too.

Since I am new to Javascript and generator, I am not sure what happened. Am I using generator correctly or did I miss something?

Thanks for any kind suggestion.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
maxym-plotnikovcommented, Oct 3, 2017

Hi! I used Node ‘co’ module for that. let job = schedule.scheduleJob(date, function(x) { co(function* () { let variable = yield … } }.bind(null,x)) P.S. Works with AdonisJS

0reactions
JhinDengcommented, Oct 10, 2017

you can try

let job = schedule.scheduleJob(date, function(x) {
  const a = function * () { console.log('hello world') }
  a.next()
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't the generators next function be a callback function ...
"Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead.". This means that yes,...
Read more >
Hanging up on Callbacks: Generators in ECMAScript 6
With generators, we can transform nested callbacks into easy-to-read top down-style code without blocking our single event loop thread.
Read more >
A Study on Solving Callbacks with JavaScript Generators
Generator methods don't return a raw value, they return an object with two properties: value and done . This makes it clear when...
Read more >
TensorBoard callback does not create histograms when a ...
My current workaround is to not pass a generator, but to exhaust it until I have the required number of validation samples. I...
Read more >
4. Generators - You Don't Know JS: Async & Performance [Book]
(no space) form. There are arguments for both styles, but I basically prefer function *foo.. because it then matches when I reference a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found