[英]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.4
和highland@^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.