Skip to content

Commit 4c17e4e

Browse files
committed
fix(perf): replace async waterfall with promise chain
1 parent 4f3d6eb commit 4c17e4e

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

index.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var async = require('async');
1+
var pify = require('pify');
22

3-
var unpack = require('./lib/unpack');
4-
var parse = require('./lib/parse');
5-
var validate = require('./lib/validate');
3+
var unpack = pify(require('./lib/unpack'));
4+
var parse = pify(require('./lib/parse'));
5+
var validate = pify(require('./lib/validate'));
66

77
/**
88
* Unpacks, parses, validates, and analyzes Scratch projects. If successful,
@@ -12,23 +12,15 @@ var validate = require('./lib/validate');
1212
* @param {Function} callback Returns error or project data
1313
*/
1414
module.exports = function (input, isSprite, callback) {
15-
// First unpack the input (need this outside of the async waterfall so that
16-
// unpackedProject can be refered to again)
17-
unpack(input, isSprite, function (err, unpackedProject) {
18-
if (err) return callback(err);
19-
20-
async.waterfall([
21-
function (cb) {
22-
parse(unpackedProject[0], cb);
23-
},
24-
// TODO is there a better way to pass this arg
25-
// than partially applying this funciton?
26-
validate.bind(null, isSprite)
27-
], function (error, validatedInput) {
28-
// One more callback wrapper so that we can re-package everything
29-
// with the possible zip returned from unpack
30-
if (error) return callback(error);
31-
callback(null, [validatedInput, unpackedProject[1]]);
32-
});
33-
});
15+
// Unpack the input and further transform the json portion by parsing and
16+
// validating it.
17+
unpack(input, isSprite)
18+
.then(function (unpackedProject) {
19+
return parse(unpackedProject[0])
20+
.then(validate.bind(null, isSprite))
21+
.then(function (validatedProject) {
22+
return [validatedProject, unpackedProject[1]];
23+
});
24+
})
25+
.then(callback.bind(null, null), callback);
3426
};

0 commit comments

Comments
 (0)