1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00
denoland-deno/std/async
2020-11-15 13:57:36 +01:00
..
deferred.ts build: migrate to dlint (#8176) 2020-11-03 16:19:29 +01:00
deferred_test.ts improve deferred test (#5459) 2020-05-15 16:59:44 -04:00
delay.ts BREAKING(std): reorganization (#5087) 2020-05-09 08:34:47 -04:00
delay_test.ts test(std/async): remove redundant export in test (#7682) 2020-09-25 13:45:57 -04:00
mod.ts feat(std/async): add pooledMap utility (#6898) 2020-07-29 02:44:34 +02:00
mux_async_iterator.ts build: migrate to dlint (#8176) 2020-11-03 16:19:29 +01:00
mux_async_iterator_test.ts build: update dlint to v0.2.10 (#8284) 2020-11-14 20:27:37 +01:00
pool.ts feat(std/async): add pooledMap utility (#6898) 2020-07-29 02:44:34 +02:00
pool_test.ts chore: add copyright (#7593) 2020-09-21 08:26:41 -04:00
README.md docs(std/async): fix a couple typos in std/async/README.md (#8388) 2020-11-15 13:57:36 +01:00
test.ts chore: add copyright (#7593) 2020-09-21 08:26:41 -04:00

async

async is a module to provide help with asynchronous tasks.

Usage

The following functions and class are exposed in mod.ts:

deferred

Create a Promise with the reject and resolve functions.

import { deferred } from "https://deno.land/std/async/mod.ts";

const p = deferred<number>();
// ...
p.resolve(42);

delay

Resolve a Promise after a given amount of milliseconds.

import { delay } from "https://deno.land/std/async/mod.ts";

// ...
const delayedPromise = delay(100);
const result = await delayedPromise;
// ...

MuxAsyncIterator

The MuxAsyncIterator class multiplexes multiple async iterators into a single stream.

The class makes an assumption that the final result (the value returned and not yielded from the iterator) does not matter. If there is any result, it is discarded.

import { MuxAsyncIterator } from "https://deno.land/std/async/mod.ts";

async function* gen123(): AsyncIterableIterator<number> {
  yield 1;
  yield 2;
  yield 3;
}

async function* gen456(): AsyncIterableIterator<number> {
  yield 4;
  yield 5;
  yield 6;
}

const mux = new MuxAsyncIterator<number>();
mux.add(gen123());
mux.add(gen456());
for await (const value of mux) {
  // ...
}
// ..

pooledMap

Transform values from an (async) iterable into another async iterable. The transforms are done concurrently, with a max concurrency defined by the poolLimit.

import { pooledMap } from "https://deno.land/std/async/mod.ts";

const results = pooledMap(
  2,
  [1, 2, 3],
  (i) => new Promise((r) => setTimeout(() => r(i), 1000)),
);

for await (const value of results) {
  // ...
}