JavaScript functional programming for promises