# Promise

WireBootstrap's `Promise` helper allows code to be more readable when working with JavaScript promises.

A normal flow requires code logic to be included inside the function passed to the `Promise` constructor.

```javascript
function () => {

  return new Promise(function(resolve, reject) {
    
      // do work...
  
      if (/* everything turned out fine */) 
        resolve("Stuff worked!");      
      else 
        reject(Error("It broke"));
      
  });

}
```

Use `wire.Promise` to create a promise object that can be used directly to resolve or reject the promise.

```javascript
function () => {
  
    const promise = new wire.Promise();
    
    // do work...
    
    if (/* everything turned out fine */) 
        promise.resolve("Stuff worked!");      
      else 
        promise.reject(Error("It broke"));
    
    return promise.promise();

}
```

## When

This is the callback version of [promise all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all).  Use `wire.when` to perform multiple operations and then call a single function.

The first parameter to `when` is the number of callbacks that are expected.  The second parameter is the function to call when all of the operations have been completed.

```javascript
function allDone() {
   console.write('all done');
}

const done = wire.when(2, allDone);

// Operation 1
function() {
   done();                    
}();

// Operation 2
function() {
   done();                    
}();   
```


---

# 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.wirebootstrap.com/wirebootstrap/utilities/promise.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.
