# Create a JavaScript function and trigger it in ConverseScript

Let's use the `functions` property:

```javascript
import code from './main.converse'

export default {
    code,
    functions: {
        nameFunctionJs() {

        }
    }
}
```

The ConverseScript script uses the function:

```typescript
nameFunctionJs()
```

If the function has asynchronous values, let's return a promise:

```javascript
import code from './main.converse'

export default {
    code,
    functions: {
        nameFunctionJs() {
            return new Promise((resolve, reject) => {
                // any code
                resolve()
            })
        }
    }
}
```

### Use ConverseScript parameters

Often, information is essential for the proper functioning of the function. For example, retrieve the user information or retrieve the user collection.

#### Using a magic variable

```javascript
import code from './main.converse'

export default {
    code,
    functions: {
        nameFunctionJs() {
            const { user } = this.converse
            // any code
            user.setMagicVariable('foo', 'hello')
        }
    }
}
```

The script is as follows:

```typescript
start() {
    nameFunctionJs()
    > { :foo }
}
```

**Property converse**

The `converse` property is an object:

```typescript
{
    users: Map<User>,
    user: User,
    data: any,
    execution: Execution,
    level: String,
    ins: Object
}
```

#### Return of a function

```javascript
import code from './main.converse'

export default {
    code,
    functions: {
        nameFunctionJs() {
            return 'hello'
        }
    }
}
```

The script is as follows:

```typescript
start() {
    str = nameFunctionJs()
    > { str }
}
```

#### Use an object

It is possible to use an object with several functions:

```javascript
import code from './main.converse'

export default {
    code,
    functions: {
        myObj: {
            nameFunction() {

            }
        }
    }
}
```

In ConverseScript

```typescript
start() {
    myObj.nameFunction()
}
```

With specific parameters:

```javascript
import code from './main.converse'

export default {
    code,
    functions: {
         myObj: {
            nameFunction() {
                const { user } = this.converse
            }
        }
    }
}
```

### Create a mock of the function

It is very useful to mock a function. Even though each function can be mocked in each unit test, it is also possible to change the behavior in a global way by associating the mock with a skill, which will be triggered during unit tests.

```javascript
import code from './main.converse'

export default {
    code,
    functions: {
         nameFunctionJs: {
            $call() {
                const { user } = this.converse
                return 'hello'
            },
            $mock() {
                const { user } = this.converse
                return 'fake'
            }
        }
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.newbot.io/use-newbot-framework-js/create-a-javascript-function-and-trigger-it-in-conversescript.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
