Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aneldev/dyna-try


https://github.com/aneldev/dyna-try

Last synced: 29 days ago
JSON representation

Awesome Lists containing this project

README

        

# dynaTry with timeout

**Example**

Suppose we have this api get method:
```
const getPerson = (id: string): Promise => {
...
}
```

Let's try to load this within 5000 seconds

```
dynaTry({
timeout: 5000,
try: () => getPerson('ID400232'),
})
.then(person => {
// Do somthing with this person
})
.catch(error => {
// Call for getPerson's errors
// or due to timeout.
});
```

Let's have specific error for the timeout.

```
dynaTry({
timeout: 5000,
try: () => getPerson('ID400232'),
timeoutError: new Error('Client timeout'),
})
.then(person => {
// Do somthing with this person
})
.catch(error => {
// Called for getPerson's errors
// In case of timeout the "Client timeout" will be here
});
```

# API

```
dynaTry = (
args: {
try: () => Promise; // The timedout promise
timeout: number; // Timeout in ms
timeoutError?: IDynaError | Error; // Custom timeout error
},
): Promise // dynaTry can resolves value
```

By default, this error is returned in timeout error.

```
dynaError({
code: 600408,
message: `Try timed out (${timeout}ms)`,
})
```

Where is the same as...
```
new Error(`Try timed out (${timeout}ms)`);
```
but without the `code`.