Class: Deferred<T>


CloudControl Pro 9 Docs / lang / Deferred

Class: Deferred<T>

lang.Deferred

A Deferred that can be completed via public functions resolve or reject. Unlike the Promise class, this class does not require the new keyword.

Example

"nodejs";
const { Deferred } = require('lang');
async function main() {
    console.log(await add(1, 2));
}
main();

function add(a, b) {
    const d = new Deferred();
    setTimeout(() => {
        d.resolve(a + b);
    }, 1000);
    return d.promise();
}

## Type parameters

| Name |
| :------ |
| `T` |

## Table of contents

### Constructors

- [constructor](lang.Deferred.md#constructor)

### Accessors

- [value](lang.Deferred.md#value)

### Methods

- [promise](lang.Deferred.md#promise)
- [reject](lang.Deferred.md#reject)
- [resolve](lang.Deferred.md#resolve)

## Constructors

### constructor

• **new Deferred**<`T`\>(`keepRunning?`)

#### Type parameters

| Name |
| :------ |
| `T` |

#### Parameters

| Name | Type | Default value |
| :------ | :------ | :------ |
| `keepRunning` | `boolean` | `true` |

## Accessors

### value

• `get` **value**(): `undefined` \| `T`

Gets the value of the Deferred, if it is pending or rejected, then returns undefined.

#### Returns

`undefined` \| `T`

## Methods

### promise

▸ **promise**(): `Promise`<`T`\>

#### Returns

`Promise`<`T`\>

___

### reject

▸ **reject**(`reason?`): `boolean`

Sets the state of the Promise to rejected using the given reason or exception. If the Promise is already fulfilled or rejected, returns false.

#### Parameters

| Name | Type |
| :------ | :------ |
| `reason?` | `any` |

#### Returns

`boolean`

___

### resolve

▸ **resolve**(`value`): `boolean`

Sets the state of the Promise to fulfilled using the given value. If the Promise is already fulfilled or rejected, returns false.

#### Parameters

| Name | Type |
| :------ | :------ |
| `value` | `T` |

#### Returns

`boolean`