fn() if resA is an Ok.
const sqrtThenToString = andThen(
(num: number): Result<string, string> =>
num < 0
? err("num must not be negative")
: ok(Math.sqrt(num).toString()),
);
expect(sqrtThenToString(ok(4))).toStrictEqual(ok("2"));
expect(sqrtThenToString(ok(-1))).toStrictEqual(err("num must not be negative"));
expect(sqrtThenToString(err("not a number"))).toStrictEqual(err("not a number"));
Returns
fn(v)ifresAis anOk(v), otherwise returns the errorresA. This is an implementation ofFlatMap. The order of arguments is reversed because of that it is useful for partial applying.