Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未执行cron作业内的承诺_Javascript_Typescript_Cron_Promise - Fatal编程技术网

Javascript 未执行cron作业内的承诺

Javascript 未执行cron作业内的承诺,javascript,typescript,cron,promise,Javascript,Typescript,Cron,Promise,我有一段typescript中的代码,它有一种非常奇怪的行为 我写了一些代码,显然是好的,但我有一个非常奇怪的行为。 在我得到loggedIn的服务后,我想开始一个CronJob SomeService.login() .then(() => { // new CronJob(expression, workFunction, null, true); workFunction(); }); function workFunction() {

我有一段typescript中的代码,它有一种非常奇怪的行为 我写了一些代码,显然是好的,但我有一个非常奇怪的行为。 在我得到loggedIn的服务后,我想开始一个CronJob

SomeService.login()
    .then(() => {
        // new CronJob(expression, workFunction, null, true);
        workFunction();
});

function workFunction() {
    console.log("start")

    Promise.resolve()
        .then(() => startSomething())
        .then(moreStuff1)
        .then(moreStuff2)
        .then(moreStuff3)
              ...
        .then(console.log)
        .catch(errorHandling);
}
如果我像现在一样调用workFunction,那么这段代码对我来说很好,但是如果我将代码切换到使用CronJob,它将调用函数,但是里面的承诺不会得到解决。 调用第一个.then,忽略下一个,并在最后一个.then()中打印函数startSomething()的返回

我不知道这是否与CronJob是从一个Promise.then()内部启动的事实有关,但我看到每个勾号都在调用workFunction,这是正确的

固定的

我可以通过将workFunction绑定到这个函数来修复它

new CronJob(expression, workFunction.bind(self), null, true);

您的
工作函数中是否缺少
返回

SomeService.login()
    .then(() => {
        // new CronJob(expression, workFunction, null, true);
        workFunction();
});

function workFunction() {
    console.log("start")

    return Promise.resolve()
        .then(() => startSomething())
        .then(moreStuff1)
        .then(moreStuff2)
        .then(moreStuff3)
              ...
        .then(console.log)
        .catch(errorHandling);
}

这种行为似乎与cron作业的执行上下文有关。我不明白,但似乎在某种程度上有所不同。我想通过超时来打破这种环境可以解决问题:

function wrapper() { setTimeout(workFunction) }

new CronJob(expression, wrapper, null, true);

由于缺少环境变量,我曾经遇到过类似的问题。支票:顺便说一下
Promise.resolve()。然后(()=>startSomething())。然后(…
就是
startSomething()。然后(…
Hi Alistair,workFunction不应该返回任何内容。但我以前也尝试过。