GUIDES Callbacks Immediate Callback
About
Binary Data
Index
Convert String to ArrayBuffer
Convert ArrayBuffer to String
Convert ArrayBuffers to String
Handle Errors Converting ArrayBuffer to String
Immutable ArrayBuffers
Resize an ArrayBuffer
Combine ArrayBuffers
Convert Base64 to Binary Data
Convert Binary Data to Base64
Convert Binary Data to Hex
Convert Hex to Binary Data
Calculate CRC for Binary Data
Compress Binary Data – One Buffer
Compress Binary Data – Streaming
Decompress Binary Data – One Buffer
Decompress Binary Data – Streaming
Callbacks
Index
One-Time Callback
Repeating Callback
Repeating Callback with Initial Delay
Immediate Callback
Reschedule Callback
Cancel Callback
Suspend Callback
What About setTimeout?
Optimizing Embedded JavaScript
Index
When to Optimize
Know Where to Optimize
Looping through an Array
Iterating Over a String
Building a String
Avoid Copying Buffers
Accessing Properties
Map versus Object
Appending to an Array
Operating on Bits
Defining Class Methods
Reducing Stack Use
Time
Index
Get Unix Time
Get Time of Day
Get Date
Get Time Since System Start
Get Microseconds
Set System Date and Time
Set Real-Time Clock Time
Get Time and Date from Network
Get Time and Date from Real-Time Clock
Sleep

Immediate Callback

Calling Timer.set() with only a callback function argument invokes the callback as soon as possible.

Timer.set(() => {
	trace("immediate callback\n");
});

The callback is not invoked immediately, but at the next opportunity when no other JavaScript is executing in the virtual machine. This has several benefits:

  • The callback runs after any other code in the function and its callers.
  • The callback runs on an empty stack, which reduces the potential for a stack overflow.
  • Dividing an operation into pieces may increase overall system responsiveness by reducing the time the JavaScript event loop is blocked.

Fans of JavaScript Promises might be tempted to use a Promise as a standard alternative to Timer.set(). This works, but is less efficient. If possible, use Timer.set() instead.

Promise.resolve().then(() => {
  trace("immediate callback\n");
});