JSFiddle - React, Tailwind, and code Playground
JavaScript
function(query, callback) {
var hasTimedOut = false;
var timer = setTimeout(function() {
// do something to handle the timeout case here
// for exampe, pass a default value to your callback
callback('it timed out!');
// also, let the program remember that
// it has timed out, so the exec-function
// wont be able to invoke the callback a second time
hasTimedOut = true;
}, 10000);
myApi.exec('SomeCommand', function(response) {
// unless we have already timed out, lets cancel the timer
// and invoke the callback with the response
if (!hasTimedout) {
clearTimeout(timer);
callback(response);
}
});
}