gildas-lormeau/zip.js — pull request #117 by someone561 (2014-12-30 18:30:30+00:00)
Title: Added inital Support for ES6 Promises

This adds inital support for the EcmaScript 6 Promises to the zip.js external API. A good instruction is given at [HTML5Rocks: JavaScript Promises](http://www.html5rocks.com/en/tutorials/es6/promises/). With this it is possible to Write a zip and unzip function like this:
```javascript
var	files = [{name: "a.txt", content: "Test"},
{name: "b.txt", content: "Test2"},
{name: "c.txt", content: "Test3"}]
zipBlob(files, function(zippedBlob) {
// unzip the data stored in zippedBlob
unzipBlob(zippedBlob, function(unzippedBlob) {
console.log(unzippedBlob) // ["Test", "Test2", "Test3"]
});
});

function zipBlob(files, callback) {
// use a zip.BlobWriter object to write zipped data into a Blob object
zip.createWriter(new zip.BlobWriter("application/zip")).then(function(zipWriter) {
// add one file after another
files.reduce(function (sequence, file) {
var blob = new Blob([ file.content ], {
type : "text/plain"
});
return sequence.then(function () {
return zipWriter.add(file.name, new zip.BlobReader(blob));
})
}, Promise.resolve()).then(function () {
// close the writer after all files zipped
zipWriter.close(callback);
});
});
}

function unzipBlob(blob, callback) {
// use a zip.BlobReader object to read zipped data stored into blob variable
zip.createReader(new zip.BlobReader(blob)).then(function(zipReader) {
// get entries from the zip file
zipReader.getEntries().then(function(entries) {
Promise.all(entries.map(function(entry) {
return entry.getData(new zip.TextWriter());
})).then(function(d) {
zipReader.close();
callback(d);
})
});
});
}

function onerror(message) {
console.error(message);
}
```
ES6 Promises Browser support can be check at http://kangax.github.io/compat-table/es6/#Promise

In development I tried to:
* Not change the existing API or the need to adjust any existing code that uses the API
* Do not change the behavior if the Browser does not support Promises
* Call the callback first and resolve the Promise afterwards
* Make the change as small as possible

I have not tested this yet in a Browser without Promise support (IE). If this is interesting I think is good to add support to the polyfill from https://github.com/jakearchibald/es6-promise#readme