1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00
denoland-deno/std/async
Bartek Iwańczuk 8e914be742
build: migrate to dlint (#8176)
This commit migrates repository from using "eslint" 
to "dlint" for linting JavaScript code.
2020-11-03 16:19:29 +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 Use dprint for internal formatting (#6682) 2020-07-14 15:24:17 -04: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: end sentences with a period in markdown (#7813) 2020-10-04 07:19:11 +11:00
test.ts chore: add copyright (#7593) 2020-09-21 08:26:41 -04:00

async

async is a module to provide help with aysncronous 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) {
  // ...
}