# dgram-as-promised

> Promisify dgram module

Latest version **6.0.0** (published 2024-06-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install dgram-as-promised
pnpm add dgram-as-promised
yarn add dgram-as-promised
bun add dgram-as-promised
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 6.0.0 |
| Published | 2024-06-22 |
| First published | 2016-07-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16.0.0 |
| Dependencies | 0 |
| Unpacked size | 26.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Piotr Roszatycki |
| Keywords | promise, dgram |

## Links

- npm: https://www.npmjs.com/package/dgram-as-promised
- Repository: https://github.com/dex4er/js-dgram-as-promised
- Homepage: http://github.com/dex4er/js-dgram-as-promised
- Issues: https://github.com/dex4er/js-dgram-as-promised/issues
- npm.io page: https://npm.io/package/dgram-as-promised

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 6.0.0 (latest) — 2024-06-22
- 5.0.1 — 2020-10-16
- 5.0.0 — 2020-10-16
- 4.0.0 — 2020-10-15
- 3.0.3 — 2019-10-08
- 3.0.2 — 2019-07-10
- 3.0.1 — 2019-06-04
- 3.0.0 — 2019-05-08
- 2.0.0 — 2018-09-09
- 1.0.6 — 2018-07-15
- 1.0.5 — 2018-07-15
- 1.0.4 — 2018-05-09
- 1.0.3 — 2018-04-03
- 1.0.2 — 2018-03-28
- 1.0.1 — 2018-03-28
- … 6 more at https://npm.io/package/dgram-as-promised/versions

## README

# dgram-as-promised

<!-- markdownlint-disable MD013 -->

[![GitHub](https://img.shields.io/github/v/release/dex4er/js-dgram-as-promised?display_name=tag&sort=semver)](https://github.com/dex4er/js-dgram-as-promised)
[![CI](https://github.com/dex4er/js-dgram-as-promised/actions/workflows/ci.yaml/badge.svg)](https://github.com/dex4er/js-dgram-as-promised/actions/workflows/ci.yaml)
[![Trunk Check](https://github.com/dex4er/js-dgram-as-promised/actions/workflows/trunk.yaml/badge.svg)](https://github.com/dex4er/js-dgram-as-promised/actions/workflows/trunk.yaml)
[![Coverage Status](https://coveralls.io/repos/github/dex4er/js-dgram-as-promised/badge.svg)](https://coveralls.io/github/dex4er/js-dgram-as-promised)
[![npm](https://img.shields.io/npm/v/dgram-as-promised.svg)](https://www.npmjs.com/package/dgram-as-promised)

<!-- markdownlint-enable MD013 -->

This module provides promisified version of the standard
[dgram](https://nodejs.org/api/dgram.html) class. The API is the same as for
standard `dgram`, except `bind`, `close` and `send` methods which return
[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
object.

## Requirements

This module requires ES2021 with Node >= 16.

## Installation

```shell
npm install dgram-as-promised
```

_Additionally for Typescript:_

```shell
npm install -D @types/node
```

## Usage

`dgram-as-promised` can be used similarly to the standard `dgram` module.

_Example:_

```js
import DgramAsPromised from "dgram-as-promised"

const socket = DgramAsPromised.createSocket("udp4")

const MEMBERSHIP = "224.0.0.1"
const PORT = 41234

const message = Buffer.from("ABCDEFGH")
```

### bind

Method `bind` returns `Promise` object which resolves to address info when
`listening` event is emitted.

```js
const address = await socket.bind()
console.log(`Socket is listening on ${address.address}:${address.port}`)

socket.setBroadcast(true)
socket.setMulticastTTL(128)

socket.addMembership(MEMBERSHIP)
console.log("Membership is set")
```

### send

Method `send` returns `Promise` object which is fulfilled when the message
has been sent.

```js
const bytes = await socket.send(message, 0, message.length, PORT, MEMBERSHIP)
console.log(`Message is sent (${bytes} bytes)`)
```

### recv

Method `recv` returns `Promise` object which resolves to the object with `msg`
and `rinfo` properties as from `message` event or resolves to `undefined` when
the socket is already closed.

It throws an error if a timeout occurs and it was set by `setTimeout` method.

```js
const packet = await socket.recv()
if (packet) {
  console.log(`Received message: ${packet.msg.toString()}`)
  console.log(`Received ${packet.rinfo.size} bytes`)
}
```

### close

Method `close` returns `Promise` object which resolves when `close` event is
emitted or the socket is already closed.

```js
await socket.close()
console.log("Socket is closed")
```

### setTimeout

```js
socket = socket.setTimeout(ms)
```

Set the timeout used by `recv` method for the idle socket and after this
timeout, the `recv` method will reject a Promise with a timeout error.

After the error, the socket is not closed and calling another `recv` method
will reset the timeout.

The method returns this object.

_Example:_

```js
socket.setTimeout(1000)
await socket.recv()
```

### iterate

Method `iterate` and the socket object returns an asynchronous iterator which
will call `recv` method until the socket is closed.

```js
for await (const packet of socket) {
  console.info(packet.msg.toString())
  // Close socket if Ctrl-D is in the message
  if (packet.msg.indexOf(4) !== -1) {
    await socket.close()
  }
}
```

### destroy

Method `destroy` cleans internal listeners.

```js
socket = socket.destroy()
```

The method returns this object.

## License

Copyright (c) 2016-2024 Piotr Roszatycki <mailto:piotr.roszatycki@gmail.com>

[MIT](https://opensource.org/licenses/MIT)

---
_Source: https://npm.io/package/dgram-as-promised · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
