将两个promise的结果输出到一个对象中(Output results of two promises into a single object)
我有两个创建承诺的异步函数。 这两个函数都使用相同的输入
i
。async function1(i) {} async function2(i) {}
我将用不同的值为我的输入多次调用这些函数,并且我希望尽可能提高代码的效率,因此我想排列承诺并使用
Promise.all()
并行运行它们。然而,我想得到一个单一的结果作为我的最终输出,这将是一个像这样的对象数组:
[ { input: i, result1: result1, result2: result2 }, ... ]
我通过两个独立的步骤完成了这个工作:
async function function1(i) { return i * i } async function function2(i) { return i * i * i } async function main() { var promises = [] for (let i = 0; i < 10; i++) { let promise = function1(i) .then(function(result1) { return {i:i, result1:result1} }); promises.push(promise) } var final = await Promise.all(promises) var promises2 = []; for (let i = 0; i < 10; i++) { let promise = function2(i) .then (function(result2) { final[i]['result2'] = result2; }); promises2.push(promise); } await Promise.all(promises2) console.log(final) } main()
不过,我觉得这可以通过一个
Promise.all()
。 你能告诉我如何?I have two asynchronous functions which create promises. Both functions use the same input
i
.async function1(i) {} async function2(i) {}
I will be calling these functions multiple times with different values for my input, and I want to make my code as efficient as possible, so I want to queue the promises and have them run in parallel using
Promise.all()
.However, I want to get a single result as my final output, which will be an array of objects like so:
[ { input: i, result1: result1, result2: result2 }, ... ]
I have accomplished this in two descrete steps:
async function function1(i) { return i * i } async function function2(i) { return i * i * i } async function main() { var promises = [] for (let i = 0; i < 10; i++) { let promise = function1(i) .then(function(result1) { return {i:i, result1:result1} }); promises.push(promise) } var final = await Promise.all(promises) var promises2 = []; for (let i = 0; i < 10; i++) { let promise = function2(i) .then (function(result2) { final[i]['result2'] = result2; }); promises2.push(promise); } await Promise.all(promises2) console.log(final) } main()
However, I feel like this can be accomplished using a single
Promise.all()
. Can you tell me how?
原文:https://stackoverflow.com/questions/50498186
最满意答案
也许如果你迭代散列键并返回包含它的数组,或者如果你创建另一个散列。 第二,它可能看起来像这样:
my %newhash; for my $key (keys %hash1) { my @list = split /, / => $hash1{$key}[0]; # or perhaps: my @list = map split(/, /, $_), @{ $hash1{$key} }; for (@list) { $newhash{$_} = $key; } } $newhash{mexico} eq 'a'; #true
这不是非常有效,但它会起作用。
Maybe if you either iterate through the hash keys and return the array containing it, or if you create another hash. For the second, it might look like this:
my %newhash; for my $key (keys %hash1) { my @list = split /, / => $hash1{$key}[0]; # or perhaps: my @list = map split(/, /, $_), @{ $hash1{$key} }; for (@list) { $newhash{$_} = $key; } } $newhash{mexico} eq 'a'; #true
This isn't terribly efficient, but it will work.
相关问答
更多-
您需要遍历第一个散列(键/值)中的散列键并累计您在另一个散列(值/计数)中找到的每个项目的计数。 如果你想将键值和重复值一起显示出来,你的第二个散列表就不会那么简单了,因为对于每个重复值,你都会得到一组键值(它们都具有相同的值)。 在这种情况下,只需将该键积累到一个数组中,然后对其元素进行计数。 也就是说,你的第二个哈希值将会是(value / [key1,key2,key3 ...]) my %hash = ( key1 => "one", key2 => "two", key3 => "one", ke ...
-
数组以散列在Perl中(array to hash in perl)[2023-02-07]
我的%散列; 用键$ K插入一个项目$ V? $ hash {$ K} = $ V 找到一个特定的名字/键$ K? if (exists $hash{$K}) { print "it is in there with value '$hash{$K}'\n"; } else { print "it is NOT in there\n" } 删除特定的名称/密钥? 删除$ hash {$ K} 将名称和日期作为关键字并将整个项目作为值? 简单的方 ... -
在Perl中创建一个散列(Creating a hash in Perl)[2023-12-05]
如果我正确地理解了你的话,这似乎可以做到这一点(最后使用Dumper()打印哈希值只是为了向你展示hashref包含的内容): #!/usr/bin/perl -w use strict; use Data::Dumper; my $dir = $ENV{PWD}; opendir( DIR, $dir ) or die $!; my @files = grep { -f "$dir/$_" } readdir( DIR ); my $hash = { $dir => { cou ... -
首先,在你的内部循环中,你有 for my $value ( @{$hash{$value1}} ) { print KEYS "$value "; } 什么是$value1 ? 我想你想用$key 。 始终use strict; use warnings use strict; use warnings来警告未定义的值和未声明的变量。 接下来,让我们看看当我们做什么时会发生什么 my %hash; push @{ $hash{ $value1[$_] } }, "(value$_)" fo ...
-
也许如果你迭代散列键并返回包含它的数组,或者如果你创建另一个散列。 第二,它可能看起来像这样: my %newhash; for my $key (keys %hash1) { my @list = split /, / => $hash1{$key}[0]; # or perhaps: my @list = map split(/, /, $_), @{ $hash1{$key} }; for (@list) { $newhash{$_} = $key; } } $newhash{ ...
-
这个简短的程序可以满足你的要求。 它将索引散列到每个数组中。 我在数组@keys使用了散列键的单独排序列表,因为直接从散列中获取它们将导致不可预知的顺序,并且看起来您需要按特定顺序输出。 use strict; use warnings; my %aggrs_by_node = ( node1 => ['a1_1','a1_2'], node2 => ['a2_1','a2_2','a2_3'], hello => ['ah_1','ah_2','ah_3'], node3 => ['a3 ...
-
我怎样才能找到数组散列的Perl散列中的最小值?(How can I find the smallest value in a Perl hash of hashes of arrays?)[2023-12-31]
my $City = { city1 => { Street1 => [ 'high_street', 2], street2 => [ 'low_street', 2], }, city2 => { Street1 => [ 'high_street1', 2], street2 => [ 'low_street2', 2], }, city3 => { Street1 => [ 'hi ... -
数组的perl散列(perl hash of arrays)[2023-09-26]
while (my ($k, $v) = each %$vsnhash) { print "$k: @$v\n"; } while (my ($k, $v) = each %$vsnhash) { print "$k: @$v\n"; } -
使用List :: Util(它是core perl的一部分)中的reduce函数。 #!/usr/bin/perl use strict; use warnings; use List::Util qw/reduce/; my %hash = ( "a" => { "b" => 2, "c" => 1, }, "d" => { "e" => 4, }, ); my $key = 'a'; print "For key: ...
-
如果你解释了你的数据中的数字是什么意思,我想这可能会更清楚 你有什么是有向图 。 原始数据仅代表每个节点的子节点,而您想要的结果列出所有后代 您必须构建一个包含所有已知信息的数据结构,并允许您查询有关两个节点之间关系的信息 我认为CPAN Graph模块套件可能会满足您的所有需求。 不幸的是,我目前正在使用我的平板电脑,因此我无法编写示例代码。 但它应该是直截了当的,如果我幸免,我会在早上检查这个问题 更新 这是我打算的代码。 在图中,所有数据点都称为边 ,所有关系都称为边 。 该程序通过分配来自%hash ...