Javascript implements waterfall flow