Skip to content

Commit

Permalink
Refactor async parallel with Promise.all
Browse files Browse the repository at this point in the history
Ref jrit#58

In this diff I refactored as small as possible part of code to replace
`async.parallel` with `Promise.all` and do not introduce any breaking
changes.

Further refactoring can be made in separate PR.
  • Loading branch information
TrySound committed Jul 1, 2020
1 parent e3effad commit 6090102
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"mocha": "^6.2.2"
},
"dependencies": {
"async": "^3.1.0",
"chalk": "^2.4.2",
"datauri": "^2.0.0",
"htmlparser2": "^4.0.0",
Expand Down
24 changes: 21 additions & 3 deletions src/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

var url = require( "url" );
var xtend = require( "xtend" );
var parallel = require( "async" ).parallel;
var path = require( "path" );
var inline = require( "./util" );

Expand Down Expand Up @@ -91,8 +90,27 @@ module.exports = function( options, callback )
index = found.index + index + 1;
}

parallel( tasks, function( err )
var promises = tasks.map( function( fn )
{
callback( err, result );
return new Promise( function( resolve, reject )
{
fn( function( error )
{
if ( error ) {
reject ( error );
} else {
resolve();
}
} );
} );
} );

Promise.all( promises )
.then( function()
{
callback( null, result );
}, function( error )
{
callback( error, result );
} );
};
24 changes: 21 additions & 3 deletions src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
var path = require( "path" );
var unescape = require( "lodash.unescape" );
var xtend = require( "xtend" );
var parallel = require( "async" ).parallel;
var inline = require( "./util" );
var css = require( "./css" );
var htmlparser = require( "htmlparser2" );
Expand Down Expand Up @@ -262,8 +261,27 @@ module.exports = function( options, callback )

result = replaceInlineAttribute( result );

parallel( tasks, function( err )
var promises = tasks.map( function( fn )
{
callback( err, result );
return new Promise( function( resolve, reject )
{
fn( function( error )
{
if ( error ) {
reject ( error );
} else {
resolve();
}
} );
} );
} );

Promise.all( promises )
.then( function()
{
callback( null, result );
}, function( error )
{
callback( error, result );
} );
};

0 comments on commit 6090102

Please sign in to comment.