njs_promise_reaction_job(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
    njs_index_t unused)
{
    njs_int_t                 ret;
    njs_bool_t                is_error;
    njs_value_t               *value, *argument, retval;
    njs_promise_reaction_t    *reaction;
    njs_promise_capability_t  *capability;

    value = njs_arg(args, nargs, 1);
    argument = njs_arg(args, nargs, 2);

    reaction = njs_data(value);
    capability = reaction->capability;

    is_error = 0;

    if (njs_is_undefined(&reaction->handler)) {
        if (reaction->type == NJS_PROMISE_REJECTED) {
            is_error = 1;
        }

        retval = *argument;

    } else {
        ret = njs_function_call(vm, njs_function(&reaction->handler),
                                &njs_value_undefined, argument, 1, &retval);
        if (njs_slow_path(ret != NJS_OK)) {
            if (njs_slow_path(njs_is_memory_error(vm, &vm->retval))) {
                return NJS_ERROR;
            }

            retval = vm->retval;
            is_error = 1;
        }
    }

    if (capability == NULL) {
        njs_vm_retval_set(vm, &retval);
        return NJS_OK;
    }

    if (is_error) {
        ret = njs_function_call(vm, njs_function(&capability->reject),
                                &njs_value_undefined, &retval, 1, &vm->retval);

    } else {
        ret = njs_function_call(vm, njs_function(&capability->resolve),
                                &njs_value_undefined, &retval, 1, &vm->retval);
    }

    if (njs_slow_path(ret != NJS_OK)) {
        return NJS_ERROR;
    }

    return NJS_OK;
}
njs_promise_reaction_job(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
    njs_index_t unused)
{
    njs_int_t                 ret;
    njs_bool_t                is_error;
    njs_value_t               *value, *argument, retval;
    njs_promise_reaction_t    *reaction;
    njs_promise_capability_t  *capability;

    value = njs_arg(args, nargs, 1);
    argument = njs_arg(args, nargs, 2);

    reaction = njs_data(value);
    capability = reaction->capability;

    is_error = 0;

    if (njs_is_undefined(&reaction->handler)) {
        if (reaction->type == NJS_PROMISE_REJECTED) {
            is_error = 1;
        }

        retval = *argument;

    } else {
        ret = njs_function_call(vm, njs_function(&reaction->handler),
                                &njs_value_undefined, argument, 1, &retval);
        if (njs_slow_path(ret != NJS_OK)) {
            if (njs_slow_path(njs_is_memory_error(vm, &vm->retval))) {
                return NJS_ERROR;
            }

            retval = vm->retval;
            is_error = 1;
        }
    }

    if (capability == NULL) {
        njs_vm_retval_set(vm, &retval);
        return NJS_OK;
    }

    if (is_error) {
        ret = njs_function_call(vm, njs_function(&capability->reject),
                                &njs_value_undefined, &retval, 1, &vm->retval);

    } else {
        ret = njs_function_call(vm, njs_function(&capability->resolve),
                                &njs_value_undefined, &retval, 1, &vm->retval);
    }

    if (njs_slow_path(ret != NJS_OK)) {
        return NJS_ERROR;
    }

    return NJS_OK;
}