PoolRequest

PoolRequest

A wrapped promise with a timeout

Constructor

new PoolRequest(timeoutInMsopt, getTimestampopt)

Source:
Example
const requestQueue = []

// A function that creates requests and stores them in an array
function createRequest(timeoutInMs) {
 const request = new PoolRequest(timeoutInMs)
 requestQueue.push(request) // store the request so it can be resolved later
 return request.getPromise() // only return the promise to the caller of makeRequest
}

// A function that creates an object and tries to resolve a request
function createThingyAndTryResolveRequest(){
 const thingy = new Thingy()
 const request = requestQueue.unshift()
 if(request && !request.didTimeout()) request.resolve(thingy)
}
Parameters:
Name Type Default Description
timeoutInMsopt number | null null

Number of milliseconds to wait before request times out. null will disable the timeout

getTimestampopt function Date.now

Function to get the current timestamp. See Date.now()

Methods

didTimeout() → {boolean}

Source:

Whether the requests has timed out

Returns:
Type
boolean

getPromise() → {Promise.<T>}

Source:

The promise for this request

Returns:
Type
Promise.<T>

hasTimeout() → {boolean}

Source:

Whether the requests was created with a timeout

Returns:
Type
boolean

reject(reasonopt)

Source:

Rejects the request and cancels the timeout if required

Parameters:
Name Type Description
reasonopt Error

reason the request was rejected

resolve(object)

Source:

Resolves the request and cancels the timeout if required

Parameters:
Name Type Description
object T

object being dispatched to request