Last active
May 1, 2018 17:50
-
-
Save magician11/cc47a7beab074896e11642370b0f5094 to your computer and use it in GitHub Desktop.
How to get a Node.js function to run once on the last day of each month
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const moment = require('moment'); | |
const someAction = () => console.log('Actioning...'); | |
const wait = ms => new Promise((resolve, reject) => setTimeout(resolve, ms)); | |
const startCron = async () => { | |
while (true) { | |
if ( | |
moment().date() === | |
moment() | |
.endOf('month') | |
.date() | |
) { | |
someAction(); | |
} | |
await wait(moment.duration(1, 'day').asMilliseconds()); | |
} | |
}; | |
console.log('Starting cron service...'); | |
startCron(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For more information on the the moment library, have a look at https://momentjs.com/
A walkthrough of this gist is here https://youtu.be/xOr6KDA-jXw