write runwhile api