njs_promise_reject(njs_vm_t *vm, njs_promise_t *promise, njs_value_t *reason)
{
    njs_int_t           ret;
    njs_queue_t         queue;
    njs_promise_data_t  *data;

    data = njs_data(&promise->value);

    data->result = *reason;
    data->state = NJS_PROMISE_REJECTED;

    if (!data->is_handled) {
        ret = njs_promise_host_rejection_tracker(vm, promise,
                                                 NJS_PROMISE_REJECT);
        if (njs_slow_path(ret != NJS_OK)) {
            return njs_value_arg(&njs_value_null);
        }
    }

    if (njs_queue_is_empty(&data->reject_queue)) {
        return njs_value_arg(&njs_value_undefined);

    } else {
        queue = data->reject_queue;

        queue.head.prev->next = &queue.head;
        queue.head.next->prev = &queue.head;
    }

    njs_queue_init(&data->fulfill_queue);
    njs_queue_init(&data->reject_queue);

    return njs_promise_trigger_reactions(vm, reason, &queue);
}
njs_promise_reject(njs_vm_t *vm, njs_promise_t *promise, njs_value_t *reason)
{
    njs_int_t           ret;
    njs_queue_t         queue;
    njs_promise_data_t  *data;

    data = njs_data(&promise->value);

    data->result = *reason;
    data->state = NJS_PROMISE_REJECTED;

    if (!data->is_handled) {
        ret = njs_promise_host_rejection_tracker(vm, promise,
                                                 NJS_PROMISE_REJECT);
        if (njs_slow_path(ret != NJS_OK)) {
            return njs_value_arg(&njs_value_null);
        }
    }

    if (njs_queue_is_empty(&data->reject_queue)) {
        return njs_value_arg(&njs_value_undefined);

    } else {
        queue = data->reject_queue;

        queue.head.prev->next = &queue.head;
        queue.head.next->prev = &queue.head;
    }

    njs_queue_init(&data->fulfill_queue);
    njs_queue_init(&data->reject_queue);

    return njs_promise_trigger_reactions(vm, reason, &queue);
}