@types/q

  • Version 1.5.4
  • Published
  • 32.1 kB
  • No dependencies
  • MIT license

Install

npm i @types/q
yarn add @types/q
pnpm add @types/q

Overview

TypeScript definitions for Q

Index

Variables

variable longStackSupport

let longStackSupport: boolean;
  • A settable property that lets you turn on long stack trace support. If turned on, "stack jumps" will be tracked across asynchronous promise operations, so that if an uncaught error is thrown by done or a rejection reason's stack property is inspected in a rejection callback, a long stack trace is produced.

Functions

function all

all: {
<A, B, C, D, E, F>(
promises: IWhenable<
[
IWhenable<A>,
IWhenable<B>,
IWhenable<C>,
IWhenable<D>,
IWhenable<E>,
IWhenable<F>
]
>
): Promise<[A, B, C, D, E, F]>;
<A, B, C, D, E>(
promises: IWhenable<
[IWhenable<A>, IWhenable<B>, IWhenable<C>, IWhenable<D>, IWhenable<E>]
>
): Promise<[A, B, C, D, E]>;
<A, B, C, D>(
promises: IWhenable<[IWhenable<A>, IWhenable<B>, IWhenable<C>, IWhenable<D>]>
): Promise<[A, B, C, D]>;
<A, B, C>(
promises: IWhenable<[IWhenable<A>, IWhenable<B>, IWhenable<C>]>
): Promise<[A, B, C]>;
<A, B>(promises: IWhenable<[IPromise<A>, IPromise<B>]>): Promise<[A, B]>;
<A, B>(promises: IWhenable<[A, IPromise<B>]>): Promise<[A, B]>;
<A, B>(promises: IWhenable<[IPromise<A>, B]>): Promise<[A, B]>;
<A, B>(promises: IWhenable<[A, B]>): Promise<[A, B]>;
<T>(promises: IWhenable<IWhenable<T>[]>): Promise<T[]>;
};
  • Returns a promise that is fulfilled with an array containing the fulfillment value of each promise, or is rejected with the same rejection reason as the first promise to be rejected.

function allResolved

allResolved: <T>(
promises: IWhenable<Array<IWhenable<T>>>
) => Promise<Promise<T>[]>;
  • Deprecated Alias for allSettled()

function allSettled

allSettled: <T>(
promises: IWhenable<Array<IWhenable<T>>>
) => Promise<PromiseState<T>[]>;
  • Returns a promise that is fulfilled with an array of promise state snapshots, but only after all the original promises have settled, i.e. become either fulfilled or rejected.

function async

async: <T>(generatorFunction: any) => (...args: any[]) => Promise<T>;
  • This is an experimental tool for converting a generator function into a deferred function. This has the potential of reducing nested callbacks in engines that support yield.

function defer

defer: <T>() => Deferred<T>;
  • Returns a "deferred" object with a: promise property resolve(value) method reject(reason) method notify(value) method makeNodeResolver() method

function delay

delay: {
<T>(promiseOrValue: T | Promise<T>, ms: number): Promise<T>;
(ms: number): Promise<void>;
};
  • Returns a promise that will have the same result as promise, but will only be fulfilled or rejected after at least ms milliseconds have passed.

  • Returns a promise that will be fulfilled with undefined after at least ms milliseconds have passed.

function denodeify

denodeify: <T>(
nodeFunction: (...args: any[]) => any,
...args: any[]
) => (...args: any[]) => Promise<T>;
  • Alias for nfbind()

function fbind

fbind: <T>(
method: (...args: any[]) => IWhenable<T>,
...args: any[]
) => (...args: any[]) => Promise<T>;
  • (Deprecated) Returns a new function that calls a function asynchronously with the given variadic arguments, and returns a promise. Notably, any synchronous return values or thrown exceptions are transformed, respectively, into fulfillment values or rejection reasons for the promise returned by this new function. This method is especially useful in its static form for wrapping functions to ensure that they are always asynchronous, and that any thrown exceptions (intentional or accidental) are appropriately transformed into a returned rejected promise. For example:

    Example 1

    var getUserData = Q.fbind(function (userName) { if (!userName) { throw new Error("userName must be truthy!"); } if (localCache.has(userName)) { return localCache.get(userName); } return getUserFromCloud(userName); });

function fcall

fcall: <T>(method: (...args: any[]) => T, ...args: any[]) => Promise<T>;
  • Returns a promise for the result of calling a function, with the given variadic arguments. Has the same return value/thrown exception translation as explained above for fbind. In its static form, it is aliased as Q.try, since it has semantics similar to a try block (but handling both synchronous exceptions and asynchronous rejections). This allows code like

    Example 1

    Q.try(function () { if (!isConnectedToCloud()) { throw new Error("The cloud is down!"); } return syncToCloud(); }) .catch(function (error) { console.error("Couldn't sync to the cloud", error); });

function invoke

invoke: <T>(obj: any, functionName: string, ...args: any[]) => Promise<T>;
  • Returns a promise for the result of calling the named method of an object with the given variadic arguments. The object itself is this in the function, just like a synchronous method call.

function isFulfilled

isFulfilled: (promise: Promise<any>) => boolean;
  • Returns whether a given promise is in the fulfilled state. When the static version is used on non-promises, the result is always true.

function isPending

isPending: (promiseOrObject: any) => boolean;
  • Returns whether a given promise is in the pending state. When the static version is used on non-promises, the result is always false.

function isPromise

isPromise: (object: any) => object is Promise<any>;
  • Returns whether the given value is a Q promise.

function isPromiseAlike

isPromiseAlike: (object: any) => object is IPromise<any>;
  • Returns whether the given value is a promise (i.e. it's an object with a then function).

function isRejected

isRejected: (promise: Promise<any>) => boolean;
  • Returns whether a given promise is in the rejected state. When the static version is used on non-promises, the result is always false.

function mcall

mcall: <T>(obj: any, functionName: string, ...args: any[]) => Promise<T>;
  • Alias for invoke()

function nbind

nbind: <T>(
nodeFunction: (...args: any[]) => any,
thisArg: any,
...args: any[]
) => (...args: any[]) => Promise<T>;
  • Creates a promise-returning function from a Node.js-style method, optionally binding it with the given variadic arguments. An example:

    Example 1

    var Kitty = mongoose.model("Kitty"); var findKitties = Q.nbind(Kitty.find, Kitty); findKitties({ cute: true }).done(function (theKitties) { //... });

function nearer

nearer: <T>(promise: Promise<T>) => T;
  • If an object is not a promise, it is as "near" as possible. If a promise is rejected, it is as "near" as possible too. If it's a fulfilled promise, the fulfillment value is nearer. If it's a deferred promise and the deferred has been resolved, the resolution is "nearer".

function nextTick

nextTick: (callback: (...args: any[]) => any) => void;

    function nfapply

    nfapply: <T>(nodeFunction: (...args: any[]) => any, args: any[]) => Promise<T>;
    • Calls a Node.js-style function with the given array of arguments, returning a promise that is fulfilled if the Node.js function calls back with a result, or rejected if it calls back with an error (or throws one synchronously). An example:

      Example 1

      Q.nfapply(FS.readFile, ["foo.txt", "utf-8"]).done(function (text) { });

      Note that this example only works because FS.readFile is a function exported from a module, not a method on an object. For methods, e.g. redisClient.get, you must bind the method to an instance before passing it to Q.nfapply (or, generally, as an argument to any function call):

      Example 2

      Q.nfapply(redisClient.get.bind(redisClient), ["user:1:id"]).done(function (user) { });

      The better strategy for methods would be to use Q.npost, as shown below.

    function nfbind

    nfbind: <T>(
    nodeFunction: (...args: any[]) => any,
    ...args: any[]
    ) => (...args: any[]) => Promise<T>;
    • Creates a promise-returning function from a Node.js-style function, optionally binding it with the given variadic arguments. An example:

      Example 1

      var readFile = Q.nfbind(FS.readFile); readFile("foo.txt", "utf-8").done(function (text) { //... });

      Note that if you have a method that uses the Node.js callback pattern, as opposed to just a function, you will need to bind its this value before passing it to nfbind, like so:

      Example 2

      var Kitty = mongoose.model("Kitty"); var findKitties = Q.nfbind(Kitty.find.bind(Kitty));

      The better strategy for methods would be to use Q.nbind, as shown below.

    function nfcall

    nfcall: <T>(nodeFunction: (...args: any[]) => any, ...args: any[]) => Promise<T>;
    • Calls a Node.js-style function with the given variadic arguments, returning a promise that is fulfilled if the Node.js function calls back with a result, or rejected if it calls back with an error (or throws one synchronously). An example:

      Example 1

      Q.nfcall(FS.readFile, "foo.txt", "utf-8").done(function (text) { });

      The same warning about functions vs. methods applies for nfcall as it does for nfapply. In this case, the better strategy would be to use Q.ninvoke.

    function ninvoke

    ninvoke: <T>(
    nodeModule: any,
    functionName: string,
    ...args: any[]
    ) => Promise<T>;
    • Calls a Node.js-style method with the given variadic arguments, returning a promise that is fulfilled if the method calls back with a result, or rejected if it calls back with an error (or throws one synchronously). An example:

      Example 1

      Q.ninvoke(redisClient, "get", "user:1:id").done(function (user) { });

    function noConflict

    noConflict: () => typeof Q;
    • Resets the global "Q" variable to the value it has before Q was loaded. This will either be undefined if there was no version or the version of Q which was already loaded before.

      Returns

      The last version of Q.

    function npost

    npost: <T>(nodeModule: any, functionName: string, args: any[]) => Promise<T>;
    • Calls a Node.js-style method with the given arguments array, returning a promise that is fulfilled if the method calls back with a result, or rejected if it calls back with an error (or throws one synchronously). An example:

      Example 1

      Q.npost(redisClient, "get", ["user:1:id"]).done(function (user) { });

    function nsend

    nsend: <T>(nodeModule: any, functionName: string, ...args: any[]) => Promise<T>;
    • Alias for ninvoke()

    function onerror

    onerror: (reason: any) => void;
    • A settable property that will intercept any uncaught errors that would otherwise be thrown in the next tick of the event loop, usually as a result of done. Can be useful for getting the full stack trace of an error in browsers, which is not usually possible with window.onerror.

    function Promise

    Promise: <T>(
    resolver: (
    resolve: (val?: IWhenable<T>) => void,
    reject: (reason?: any) => void,
    notify: (progress: any) => void
    ) => void
    ) => Promise<T>;
    • Synchronously calls resolver(resolve, reject, notify) and returns a promise whose state is controlled by the functions passed to resolver. This is an alternative promise-creation API that has the same power as the deferred concept, but without introducing another conceptual entity. If resolver throws an exception, the returned promise will be rejected with that thrown exception as the rejection reason. note: In the latest github, this method is called Q.Promise, but if you are using the npm package version 0.9.7 or below, the method is called Q.promise (lowercase vs uppercase p).

    function promised

    promised: <T>(callback: (...args: any[]) => T) => (...args: any[]) => Promise<T>;
    • Creates a new version of func that accepts any combination of promise and non-promise values, converting them to their fulfillment values before calling the original func. The returned version also always returns a promise: if func does a return or throw, then Q.promised(func) will return fulfilled or rejected promise, respectively. This can be useful for creating functions that accept either promises or non-promise values, and for ensuring that the function always returns a promise even in the face of unintentional thrown exceptions.

    function Q

    Q: typeof Q;
    • If value is a Q promise, returns the promise. If value is a promise from another library it is coerced into a Q promise (where possible). If value is not a promise, returns a promise that is fulfilled with value.

    • Calling with nothing at all creates a void promise

    function race

    race: <T>(promises: Array<IWhenable<T>>) => Promise<T>;
    • Returns a promise for the first of an array of promises to become settled.

    function reject

    reject: <T>(reason?: any) => Promise<T>;
    • Returns a promise that is rejected with reason.

    function resolve

    resolve: <T>(object?: IWhenable<T>) => Promise<T>;
    • Calling resolve with a pending promise causes promise to wait on the passed promise, becoming fulfilled with its fulfillment value or rejected with its rejection reason (or staying pending forever, if the passed promise does). Calling resolve with a rejected promise causes promise to be rejected with the passed promise's rejection reason. Calling resolve with a fulfilled promise causes promise to be fulfilled with the passed promise's fulfillment value. Calling resolve with a non-promise value causes promise to be fulfilled with that value.

    function send

    send: <T>(obj: any, functionName: string, ...args: any[]) => Promise<T>;
    • Alias for invoke()

    function spread

    spread: <T, U>(
    promises: Array<IWhenable<T>>,
    onFulfilled: (...args: T[]) => IWhenable<U>,
    onRejected?: (reason: any) => IWhenable<U>
    ) => Promise<U>;
    • Like then, but "spreads" the array into a variadic fulfillment handler. If any of the promises in the array are rejected, instead calls onRejected with the first rejected promise's rejection reason. This is especially useful in conjunction with all.

    function timeout

    timeout: <T>(promise: Promise<T>, ms: number, message?: string) => Promise<T>;
    • Returns a promise that will have the same result as promise, except that if promise is not fulfilled or rejected before ms milliseconds, the returned promise will be rejected with an Error with the given message. If message is not supplied, the message will be "Timed out after " + ms + " ms".

    function try

    try: <T>(method: (...args: any[]) => T, ...args: any[]) => Promise<T>
    • Returns a promise for the result of calling a function, with the given variadic arguments. Has the same return value/thrown exception translation as explained above for fbind. In its static form, it is aliased as Q.try, since it has semantics similar to a try block (but handling both synchronous exceptions and asynchronous rejections). This allows code like

      Example 1

      Q.try(function () { if (!isConnectedToCloud()) { throw new Error("The cloud is down!"); } return syncToCloud(); }) .catch(function (error) { console.error("Couldn't sync to the cloud", error); });

    function when

    when: {
    (): Promise<void>;
    <T>(value: IWhenable<T>): Promise<T>;
    <T, U>(
    value: IWhenable<T>,
    onFulfilled: (val: T) => IWhenable<U>,
    onRejected?: (reason: any) => IWhenable<U>,
    onProgress?: (progress: any) => any
    ): Promise<U>;
    };

      Interfaces

      interface Deferred

      interface Deferred<T> {}

        property promise

        promise: Promise<T>;

          method makeNodeResolver

          makeNodeResolver: () => (reason: any, value: T) => void;
          • Returns a function suitable for passing to a Node.js API. That is, it has a signature (err, result) and will reject deferred.promise with err if err is given, or fulfill it with result if that is given.

          method notify

          notify: (value: any) => void;
          • Calling notify with a value causes promise to be notified of progress with that value. That is, any onProgress handlers registered with promise or promises derived from promise will be called with the progress value.

          method reject

          reject: (reason?: any) => void;
          • Calling reject with a reason causes promise to be rejected with that reason.

          method resolve

          resolve: (value?: IWhenable<T>) => void;
          • Calling resolve with a pending promise causes promise to wait on the passed promise, becoming fulfilled with its fulfillment value or rejected with its rejection reason (or staying pending forever, if the passed promise does). Calling resolve with a rejected promise causes promise to be rejected with the passed promise's rejection reason. Calling resolve with a fulfilled promise causes promise to be fulfilled with the passed promise's fulfillment value. Calling resolve with a non-promise value causes promise to be fulfilled with that value.

          interface Promise

          interface Promise<T> {}

            method catch

            catch: <U>(onRejected: (reason: any) => IWhenable<U>) => Promise<U>;
            • A sugar method, equivalent to promise.then(undefined, onRejected).

            method delay

            delay: (ms: number) => Promise<T>;
            • Returns a promise that will have the same result as promise, but will only be fulfilled or rejected after at least ms milliseconds have passed.

            method delete

            delete: <U>(propertyName: string) => Promise<U>;

              method done

              done: (
              onFulfilled?: (value: T) => any,
              onRejected?: (reason: any) => any,
              onProgress?: (progress: any) => any
              ) => void;
              • Much like then, but with different behavior around unhandled rejection. If there is an unhandled rejection, either because promise is rejected and no onRejected callback was provided, or because onFulfilled or onRejected threw an error or returned a rejected promise, the resulting rejection reason is thrown as an exception in a future turn of the event loop. This method should be used to terminate chains of promises that will not be passed elsewhere. Since exceptions thrown in then callbacks are consumed and transformed into rejections, exceptions at the end of the chain are easy to accidentally, silently ignore. By arranging for the exception to be thrown in a future turn of the event loop, so that it won't be caught, it causes an onerror event on the browser window, or an uncaughtException event on Node.js's process object. Exceptions thrown by done will have long stack traces, if Q.longStackSupport is set to true. If Q.onerror is set, exceptions will be delivered there instead of thrown in a future turn. The Golden Rule of done vs. then usage is: either return your promise to someone else, or if the chain ends with you, call done to terminate it. Terminating with catch is not sufficient because the catch handler may itself throw an error.

              method fail

              fail: <U>(onRejected: (reason: any) => IWhenable<U>) => Promise<U>;
              • Alias for catch() (for non-ES5 browsers)

              method fapply

              fapply: <U>(args: any[]) => Promise<U>;
              • Returns a promise for the result of calling a function, with the given array of arguments. Essentially equivalent to

                Example 1

                promise.then(function (f) { return f.apply(undefined, args); });

              method fcall

              fcall: <U>(...args: any[]) => Promise<U>;
              • Returns a promise for the result of calling a function, with the given variadic arguments. Has the same return value/thrown exception translation as explained above for fbind. In its static form, it is aliased as Q.try, since it has semantics similar to a try block (but handling both synchronous exceptions and asynchronous rejections). This allows code like

                Example 1

                Q.try(function () { if (!isConnectedToCloud()) { throw new Error("The cloud is down!"); } return syncToCloud(); }) .catch(function (error) { console.error("Couldn't sync to the cloud", error); });

              method fin

              fin: (finallyCallback: () => any) => Promise<T>;
              • Alias for finally() (for non-ES5 browsers)

              method finally

              finally: (finallyCallback: () => any) => Promise<T>;
              • Like a finally clause, allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful for collecting resources regardless of whether a job succeeded, like closing a database connection, shutting a server down, or deleting an unneeded key from an object. finally returns a promise, which will become resolved with the same fulfillment value or rejection reason as promise. However, if callback returns a promise, the resolution of the returned promise will be delayed until the promise returned from callback is finished. Furthermore, if the returned promise rejects, that rejection will be passed down the chain instead of the previous result.

              method get

              get: <U>(propertyName: string) => Promise<U>;
              • Returns a promise to get the named property of an object. Essentially equivalent to

                Example 1

                promise.then(function (o) { return o[propertyName]; });

              method inspect

              inspect: () => PromiseState<T>;
              • Returns a "state snapshot" object, which will be in one of three forms:

                - { state: "pending" } - { state: "fulfilled", value: <fulfllment value> } - { state: "rejected", reason: <rejection reason> }

              method invoke

              invoke: <U>(methodName: string, ...args: any[]) => Promise<U>;
              • Returns a promise for the result of calling the named method of an object with the given variadic arguments. The object itself is this in the function, just like a synchronous method call.

              method isFulfilled

              isFulfilled: () => boolean;
              • Returns whether a given promise is in the fulfilled state. When the static version is used on non-promises, the result is always true.

              method isPending

              isPending: () => boolean;
              • Returns whether a given promise is in the pending state. When the static version is used on non-promises, the result is always false.

              method isRejected

              isRejected: () => boolean;
              • Returns whether a given promise is in the rejected state. When the static version is used on non-promises, the result is always false.

              method keys

              keys: () => Promise<string[]>;
              • Returns a promise for an array of the property names of an object. Essentially equivalent to

                Example 1

                promise.then(function (o) { return Object.keys(o); });

              method nodeify

              nodeify: (callback: (reason: any, value: any) => void) => Promise<T>;
              • If callback is a function, assumes it's a Node.js-style callback, and calls it as either callback(rejectionReason) when/if promise becomes rejected, or as callback(null, fulfillmentValue) when/if promise becomes fulfilled. If callback is not a function, simply returns promise.

              method post

              post: <U>(methodName: string, args: any[]) => Promise<U>;
              • Returns a promise for the result of calling the named method of an object with the given array of arguments. The object itself is this in the function, just like a synchronous method call. Essentially equivalent to

                Example 1

                promise.then(function (o) { return o[methodName].apply(o, args); });

              method progress

              progress: (onProgress: (progress: any) => any) => Promise<T>;
              • A sugar method, equivalent to promise.then(undefined, undefined, onProgress).

              method set

              set: <U>(propertyName: string, value: any) => Promise<U>;

                method spread

                spread: <U>(
                onFulfill: (...args: any[]) => IWhenable<U>,
                onReject?: (reason: any) => IWhenable<U>
                ) => Promise<U>;
                • Like then, but "spreads" the array into a variadic fulfillment handler. If any of the promises in the array are rejected, instead calls onRejected with the first rejected promise's rejection reason. This is especially useful in conjunction with all

                method tap

                tap: (onFulfilled: (value: T) => any) => Promise<T>;
                • Attaches a handler that will observe the value of the promise when it becomes fulfilled, returning a promise for that same value, perhaps deferred but not replaced by the promise returned by the onFulfilled handler.

                method then

                then: {
                <U>(
                onFulfill?: (value: T) => IWhenable<U>,
                onReject?: (error: any) => IWhenable<U>,
                onProgress?: (progress: any) => any
                ): Promise<U>;
                <U = T, V = never>(
                onFulfill?: (value: T) => IWhenable<U>,
                onReject?: (error: any) => IWhenable<V>,
                onProgress?: (progress: any) => any
                ): Promise<U | V>;
                };
                • The then method from the Promises/A+ specification, with an additional progress handler.

                method thenReject

                thenReject: <U = T>(reason?: any) => Promise<U>;
                • A sugar method, equivalent to promise.then(function () { throw reason; }).

                method thenResolve

                thenResolve: <U>(value: U) => Promise<U>;
                • A sugar method, equivalent to promise.then(function () { return value; }).

                method timeout

                timeout: (ms: number, message?: string) => Promise<T>;
                • Returns a promise that will have the same result as promise, except that if promise is not fulfilled or rejected before ms milliseconds, the returned promise will be rejected with an Error with the given message. If message is not supplied, the message will be "Timed out after " + ms + " ms".

                method valueOf

                valueOf: () => any;

                  interface PromiseState

                  interface PromiseState<T> {}

                    property reason

                    reason?: any;

                      property state

                      state: 'fulfilled' | 'rejected' | 'pending';

                        property value

                        value?: T;

                          Type Aliases

                          type IPromise

                          type IPromise<T> = PromiseLike<T>;

                            type IWhenable

                            type IWhenable<T> = PromiseLike<T> | T;

                              Package Files (1)

                              Dependencies (0)

                              No dependencies.

                              Dev Dependencies (0)

                              No dev dependencies.

                              Peer Dependencies (0)

                              No peer dependencies.

                              Badge

                              To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                              You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@types/q.

                              • Markdown
                                [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@types/q)
                              • HTML
                                <a href="https://www.jsdocs.io/package/@types/q"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>