# 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();                    
}();   
```
