solve the while logic in callback function