Refactor code with promises