• Determines the current state of a Promise.

    Parameters

    • p: Promise<any>

      The Promise whose state is to be determined.

    Returns Promise<"pending" | "resolved" | "rejected">

    A Promise that resolves to a string representing the state of the input Promise:

    • 'pending': The input Promise has not settled yet.
    • 'resolved': The input Promise has resolved successfully.
    • 'rejected': The input Promise has been rejected.

    Throws

    If the input is not a Promise.

    Example

    const myPromise = new Promise(resolve => setTimeout(() => resolve('done'), 1000));

    // Check state immediately
    promiseState(myPromise).then(state => console.log(state)); // Logs: 'pending'

    // Check state after 2 seconds
    setTimeout(() => {
    promiseState(myPromise).then(state => console.log(state)); // Logs: 'resolved'
    }, 2000);

    Remarks

    This function uses Promise.race() internally to determine the state of the input Promise. It does not affect the execution or result of the input Promise in any way.

    Note that the state of a Promise can change from 'pending' to either 'resolved' or 'rejected', but once it's settled (either 'resolved' or 'rejected'), it cannot change again.