# toProvider() closures

The mapping destination `toProvider()` can also take a closure that will be executed whenever that mapping is requested. This allows you to add your own custom provider construction code inline without creating a standalone provider object that implements our provider interface. By leveraging closures you can really get funky and more concise in your coding. This closure will have the following signature and it must return the instance requested:

```
/**
* Create an instance of an object
* @injector The WireBox injector reference
*
* @return Any instance object
*/
function( injector ){}
```

Here is an example of how to accomplish this:

```javascript
map("MyEspresso").toProvider( function( injector ){
    var espresso = new Espresso( sugar=2, cream = true );
    arguments.injector.autowire( espresso );
    return espresso;
} );
```


---

# 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://wirebox.ortusbooks.com/advanced-topics/providers/toprovider-closures.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.
