diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-07-03 21:45:55 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-07-03 21:45:55 +0100 |
| commit | cd3b84e15ea1a47ff8ebcca18d68ef0cdccf20b8 (patch) | |
| tree | 46c9fbd816c26116fbc36e7578b763162050d69a /lib | |
| parent | 792aa9fb8cab1b68c585f09336758e2c76e3e8d5 (diff) | |
Add also function as a lesser evil to avoid callback hell
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/docx/docx-reader.js | 57 | ||||
| -rw-r--r-- | lib/promises.js | 9 |
2 files changed, 38 insertions, 28 deletions
diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js index 554e329..80842d7 100644 --- a/lib/docx/docx-reader.js +++ b/lib/docx/docx-reader.js @@ -14,35 +14,36 @@ var notesReader = require("./notes-reader"); function read(options) { - var docxFile = unzip.openZip(options); - return promises.when(docxFile).then(function(docxFile) { - return promises.all([ - readRelationshipsFromZipFile(docxFile), - readContentTypesFromZipFile(docxFile), - readNumberingFromZipFile(docxFile), - readStylesFromZipFile(docxFile) - ]).spread(function(relationships, contentTypes, numbering, styles) { - var bodyReader = new BodyReader({ - relationships: relationships, - contentTypes: contentTypes, - docxFile: docxFile, - numbering: numbering, - styles: styles - }); - return promises.all([ - bodyReader, - readFootnotesFromZipFile(docxFile), - readEndnotesFromZipFile(docxFile), - readXmlFromZipFile(docxFile, "word/document.xml") - ]); - }).spread(function(bodyReader, footnotes, endnotes, documentXml) { - var reader = new DocumentXmlReader({ - bodyReader: bodyReader, - footnotes: footnotes, - endnotes: endnotes - }); - return reader.convertXmlToDocument(documentXml); + return promises.props({ + docxFile: unzip.openZip(options) + }).also(function(result) { + return { + relationships: readRelationshipsFromZipFile(result.docxFile), + contentTypes: readContentTypesFromZipFile(result.docxFile), + numbering: readNumberingFromZipFile(result.docxFile), + styles: readStylesFromZipFile(result.docxFile) + }; + }).also(function(result) { + var bodyReader = new BodyReader({ + relationships: result.relationships, + contentTypes: result.contentTypes, + docxFile: result.docxFile, + numbering: result.numbering, + styles: result.styles + }); + return { + documentBodyReader: bodyReader, + footnotes: readFootnotesFromZipFile(result.docxFile), + endnotes: readEndnotesFromZipFile(result.docxFile), + documentXml: readXmlFromZipFile(result.docxFile, "word/document.xml") + }; + }).then(function(result) { + var reader = new DocumentXmlReader({ + bodyReader: result.documentBodyReader, + footnotes: result.footnotes, + endnotes: result.endnotes }); + return reader.convertXmlToDocument(result.documentXml); }); } diff --git a/lib/promises.js b/lib/promises.js index 747bc5b..4c10285 100644 --- a/lib/promises.js +++ b/lib/promises.js @@ -1,9 +1,11 @@ +var _ = require("underscore"); var bluebird = require("bluebird/js/main/promise")(); exports.defer = bluebird.defer; exports.when = bluebird.resolve; exports.resolve = bluebird.resolve; exports.all = bluebird.all; +exports.props = bluebird.props; exports.reject = bluebird.reject; exports.promisify = bluebird.promisify; @@ -14,3 +16,10 @@ exports.nfcall = function(func) { }; bluebird.prototype.fail = bluebird.prototype.caught; + +bluebird.prototype.also = function(func) { + return this.then(function(value) { + var returnValue = _.extend({}, value, func(value)); + return bluebird.props(returnValue); + }); +}; |
