supermacro/neverthrow

[question] how to combine the sync and async api?

unknown1337 opened this issue · 3 comments

I have a parent function that returns a resultasync as it executes at least one async child function. the two child functions to chain return a Result and a ResultAsync respectively.

question, how can I combine Result and ResultAsync ?

Thanks!

function syncValidation():Result<string,Error>{.... perform e.g. input validation}
function asyncFetch():ResultAsync<string,Error>{.... perform e.g. network call}

function parent(id:string):ResultAsync<string,Error>{
  return syncValidation().andThen(id=>asyncFetch(id))   // <---- this line
}

Do i need to create helper functions myselve to wrap the syncValidation function or is there some smarter way?

function wrapResult<T, E>(result: Result<T, E>): ResultAsync<T, E> {
  if (result.isOk()) return okAsync(result.value);
  else return errAsync(result.error);
}

thanks, solved, completely missed that!

close