未执行cron作业内的承诺
问题描述:
我有一个脚本代码中有一个非常奇怪的行为 我写了一些代码,显然是好的,但我有一个非常奇怪的行为。 我登录到服务后,我想启动一个CronJob。未执行cron作业内的承诺
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);
}
此代码工作正常的我,如果我只是叫功函数,喜欢它的权利,但如果切换的代码使用的cronjob,它会调用该函数,但承诺内不会得到解决。 第一个.then被调用,接下来的被忽略,函数startSomething()的返回将被打印在最后.then()
我不知道这是否与CronJob从Promise.then()开始,但是我看到work函数在每个tick上被调用,这是正确的。
FIXED
我可以功函数结合这一解决它。
new CronJob(expression, workFunction.bind(self), null, true);
答
你难道没有一个return
缺少你workFunction
?
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);
}
+0
嗨Alistair,workFunction不应该返回任何事情,但我也曾尝试过。 –
答
看来这种行为与cron作业的执行上下文有某种关系。我不明白,但它在某种程度上似乎有所不同。我想通过超时解决这个问题解决了这个问题:
function wrapper() { setTimeout(workFunction) }
new CronJob(expression, wrapper, null, true);
由于缺少环境变量,我曾经有类似的问题。检查:http://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work和http://2clickfix.com/6-reasons-cron-job-not-running/ – javier
顺便说一下...()=> startSomething())。然后(...'只是'startSomething()。然后(...' –