Title: Node.js prevent stacking of async functions within setInterval
Tags: mysql;node.js
Question: I have a set interval running a SQL query quickly. The query on average take .0003 seconds to complete. However there is the possibility that in rare circumstances the query results could exceed 100ms.
setInterval(function(){
Data = sql.Query(SqlData);
},1000/30);
I want to ensure that we have no more than one sql.Query(SqlData) being done at a single time.
Comment: The answer depends on what exactly should happen when there's pending query. Should an interval be skipped or be shifted until query completion? Should it bail out on error? What is `sql`? Promise control flow is beneficial here.

Comment: You should use `setTimeout` instead of `setInterval` in that case. You will create the timeout as soon as the query finished.

Comment: if i nest a setTimeout wont that prevent other functions within the set Interval from executing?

Comment: @MoonEater916 mcdonaldheather@example.net. calling setTimeout "recursively" is the usual procedure


Here is another answer: you could just start the query again in the callback.
query();
function query(){
Data = sql.Query(SqlData, function(){
query();
});
so you are safe that only one query runs at a time

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:07/27/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<mysql><node.js>', 'Title': 'Node.js prevent stacking of async functions within setInterval', 'ViewCount': 37, 'id': 51552832, 'user_AboutMe': '<p>Just a IT guy.</p>\n', 'user_AccountId': 11092614.0, 'user_DisplayName': 'MoonEater916', 'user_Reputation': 396.0, 'user_Views': 67.0, 'user_id': 8218884.0}}