write file before emiting and calling back