Skip to content

Instantly share code, notes, and snippets.

@guhou
Created March 6, 2020 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guhou/8199a7dc2f580e420acd811d143ea6a4 to your computer and use it in GitHub Desktop.
Save guhou/8199a7dc2f580e420acd811d143ea6a4 to your computer and use it in GitHub Desktop.
What is the name of this `unSequence` function?
export function sequence<T>(
map: Map<string, Promise<T>>
): Promise<Map<string, T>> {
return Promise.all(
Array.from(map, ([key, promisedValue]) =>
promisedValue.then<[string, T]>(value => [key, value])
)
).then((entries) => new Map(entries));
}
export function unSequence<T>(
promisedMap: Promise<Map<string, T>>,
keys: string[]
): Map<string, Promise<T | undefined>> {
return new Map(
keys.map(key =>
[key, promisedMap.then(map => map.get(key))]
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment