Refactor callback hell

It could be a local function.