Generic JavaScript class for creating an iterator that yields the UI thread via setImmediate