简体   繁体   English

从高原流中的aync函数返回时,如何处理元组?

[英]How to deal with tuples when returned from an aync function within highland streams?

I use tsc@2.3.4 and highland@^2.13.0 . 我使用tsc@2.3.4highland@^2.13.0

I have an async function that returns a tuple of the type [string, string[]] . 我有一个异步函数,返回一个[string, string[]]类型的元组。

I have worked with highland in the past, and I know that I can work with promises by creating a new highland stream with the promise, and resolving the promise through flattening it. 过去,我曾在高地工作过,我知道我可以通过创建带有承诺的新高地流并通过展平承诺来解决承诺。

So what I am doing is basically: creating promises, parallelizing their consumption, and in the end I want one stream of [string, string[]] via flatten . 因此,我要做的基本上是:创建promise,并行化它们的使用,最后我希望通过flatten一个[string, string[]]流。

Yet highland actual behavior contradicts my expectation. 然而高地的实际行为与我的预期相矛盾。

Here my TypeScript code showcasing my expectation: 在这里,我的TypeScript代码展示了我的期望:

import * as highland from "highland";

const somethingAsync = async (s: string): Promise<[string, string[]]> => {
    return [s, s.split("")];
};

const data = [
    "foo",
    "bar",
    "baz",
    "poit",
    "gnarf",
];

const stream: Highland.Stream<[string, string[]]> = highland(data)
    .map((input: string) => highland(somethingAsync(input))) // wrapping the promises into a highland stream
    .parallel(3) // processing it in parallel
    .flatten(); // flattening it to resolve the promises

stream.each((shouldBeATuple: [string, string[]]) => {
    console.log(shouldBeATuple);

    const [item1, item2] = shouldBeATuple;
    console.log(item1, item2);
});

I expect shouldBeATuple to actually be a tuple. 我希望shouldBeATuple实际上是一个元组。 Yet instead I get a stream of all the elements, as if flatten flattens too much. 但是,相反,我得到了所有元素的流,好像flatten化了太多。

I expect it to be on first iteration: 我希望它是第一次迭代:

 ["foo", ["f","o","o"]];

and on second one: 在第二个:

 ["bar", ["b","a","r"]];

Yet what I get instead is: 但是我得到的是:

 "foo",

and then it will be followed by: "f", "o", "o", "bar", "b", "a", "r". 然后跟着:“ f”,“ o”,“ o”,“ bar”,“ b”,“ a”,“ r”。

So I lose the tuples completely and get one big "mess" of a stream with all the elements in it. 因此,我完全失去了元组,并获得其中包含所有元素的流的一个大“混乱”。

How can I get a stream of tuples when using async functions? 使用异步函数时如何获取元组流?


I found out that when I do not flatten the stream I get my result yet typescript will throw an error: 我发现当我不展平流时,我得到了结果,但打字稿将抛出错误:

15 const stream: Highland.Stream<[string, string[]]> = highland(data)
         ~~~~~~
src/tuple.ts(15,7): error TS2322: Type 'Stream<Stream<[string, string[]]>>' is not assignable to type 'Stream<[string, string[]]>'.
  Type 'Stream<[string, string[]]>' is not assignable to type '[string, string[]]'.
    Property '0' is missing in type 'Stream<[string, string[]]>'.

The type declaration for parallel is wrong. parallel的类型声明是错误的。 When it is corrected, your code passes the type checker without the flatten . 更正后,您的代码将通过类型检查器而没有flatten I've submitted a pull request to fix the typings; 我已经提交了一个拉动请求,以修复打字问题; you can add a copy of the corrected typings from there to your project. 您可以从那里将更正的类型的副本添加到您的项目中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从承诺创建高原流时,如何处理承诺拒绝? - How to handle promise rejection when creating a highland stream from a promise? 如何使用highland.js派生流? - How to fork a stream using highland.js? 如何以函数返回的角度显示对象 - How to display an object in angular returned from a function 打字稿:如何声明具有从 IIFE 返回的自定义属性的函数的类型 - Typescript: How to declare the type for a function with custom properties that is returned from an IIFE 如何监视从 Jest 中的模拟返回的 object function? - How to spy on an object function returned from a mock in Jest? 如何获得从 function 返回的 object 类型 - how can get object type who returned from a function 使用rest参数将参数转发给柯里化函数时如何处理可选元组元素(使用strictNullChecking) - How to deal with optional tuple element when using rest parameters to forward arguments to curried function (with strictNullChecking) 如何将函数绑定到从类方法返回的对象 - How to bind a function to an object that is returned from a class method 如何从JSON对象中触发函数 - How to fire a function from within a JSON object 如何使用typescript在angular 2应用程序中使用aync / await - how to use aync/await in angular 2 application using typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM