我有一個腳本代碼中有一個非常奇怪的行爲 我寫了一些代碼,顯然是好的,但我有一個非常奇怪的行爲。 我登錄到服務後,我想啓動一個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);
由於缺少環境變量,我曾經有類似的問題。檢查:http://askubuntu.com/questions/23009/reasons-why-crontab-does-not-work和http://2clickfix.com/6-reasons-cron-job-not-running/ – javier
順便說一下...()=> startSomething())。然後(...'只是'startSomething()。然後(...' –