Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to write the type of an async pipe function? #31

Closed
joshuaavalon opened this issue Aug 9, 2019 · 6 comments
Closed

How to write the type of an async pipe function? #31

joshuaavalon opened this issue Aug 9, 2019 · 6 comments
Assignees
Labels
question More info is requested wiki It's nice to learn stuff

Comments

@joshuaavalon
Copy link

joshuaavalon commented Aug 9, 2019

馃 Question

Describe your question

How to write the type of an async pipe function?

Implementation

const pipePromises = (...fns :any) => (x:any) => fns.reduce((p:any, fn:any) => p.then(fn), Promise.resolve(x));
const a = (a1: number) => `${a1}`;
const b = async (a1: string) => [a1];
pipePromises(a, b)(1).then((v: any) => console.log(v)); // ["1"]

pipePromises accepts normal function and async function. I know that the normal pipe function can use <Fns extends F.Function[]>(...args: F.Piper<Fns>): F.Pipe<Fns> but I don't know how to write the promise version.

Search tags, topics

#promise, #async

@joshuaavalon joshuaavalon changed the title How to write the type of an async function? How to write the type of an async pipe function? Aug 9, 2019
@millsp millsp self-assigned this Aug 9, 2019
@millsp millsp added the question More info is requested label Aug 9, 2019
@millsp
Copy link
Owner

millsp commented Aug 9, 2019

td;dr It's now in the docs

Hi @joshuaavalon,

I think it handles it by default:

const a = (a1: number) => `${a1}`;
const b = async (a1: string) => [a1];

const pipePromises = <Fns extends F.Function[]>(...fns: F.Piper<Fns>): F.Pipe<Fns> => {
  return ((x: any) => fns.reduce((p: any, fn: any) => p.then(fn), Promise.resolve(x))) as F.Pipe<Fns>
}

const t = pipePromises(a, b)(1).then((v: any) => console.log(v)); // ["1"]

or even

const a = async (a1: number) => `${a1}`;
const b = async (a1: Promise<string>) => [a1];

const pipePromises = <Fns extends F.Function[]>(...fns: F.Piper<Fns>): F.Pipe<Fns> => {
  return ((x: any) => fns.reduce((p: any, fn: any) => p.then(fn), Promise.resolve(x))) as F.Pipe<Fns>
}

const t = pipePromises(a, b)(1).then((v: any) => console.log(v)); // ["1"]

@millsp
Copy link
Owner

millsp commented Aug 9, 2019

Use as to force a type cast

@millsp millsp closed this as completed Aug 9, 2019
@millsp millsp added the wiki It's nice to learn stuff label Aug 9, 2019
@joshuaavalon
Copy link
Author

@pirix-gh I may failed to mention something in my example. The implementation actually unwrap the promise of the result.

Consider the following

const a = async (a1: number) => `${a1}`;
const b = async (a1: string) => [a1];

This works because JavaScript automatically unwrap promise in then. const b = async (a1: Promise<string>) => [a1]; is actually wrong because it will receive string instead of Promise<string>.

@millsp
Copy link
Owner

millsp commented Aug 10, 2019

Yhea, I see what you mean now:

import {O, T, F} from 'ts-toolbelt';

const a = async (a1: number) => `${a1}`;
const b = async (b1: Promise<string>) => [b1];
const c = async (c1: Promise<Promise<string>[]>) => [c1];

const pipePromises = <Fns extends F.Function[]>(...fns: F.Piper<Fns>): F.Pipe<Fns> => {
  return ((x: any) => fns.reduce((p: any, fn: any) => p.then(fn), Promise.resolve(x))) as F.Pipe<Fns>
}

const t = pipePromises(a, b, c)(1).then((v: any) => console.log(v)); // ["1"]

However, this is still correct, but yours unwraps the value as it reduces the list.

I'll see what I can think of. I think you'll need a specially crafted type for this.

@millsp millsp reopened this Aug 10, 2019
@millsp
Copy link
Owner

millsp commented Aug 10, 2019

@joshuaavalon, good news!

You now have an async option that will unwrap the promise types.

Check the docs out. And let me know if it's all good :)

@millsp millsp closed this as completed Aug 10, 2019
@millsp
Copy link
Owner

millsp commented Aug 12, 2019

@joshuaavalon did it work out for you?

Repository owner locked and limited conversation to collaborators Feb 2, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question More info is requested wiki It's nice to learn stuff
Projects
None yet
Development

No branches or pull requests

2 participants