Node.js Asynchronous Library Comparison - Q vs Async

前端 未结 3 1322
梦谈多话
梦谈多话 2020-12-08 07:35

I have used kriskowal\'s Q library for a project (web scraper / human-activity simulator) and have become acquainted with promises, returning them and resolving/rejecting th

3条回答
  •  时光说笑
    2020-12-08 07:59

    Both libraries are good. I have discovered that they serve separate purposes and can be used in tandem.

    Q provides the developer with promise objects, which are future representations of values. Useful for time travelling.

    Async provides the developer with asynchronous versions of control structures and aggregate operations.

    An example from one attempt at a linter implementation demonstrates a potential unity among libraries:

    function lint(files, callback) {
    
        // Function which returns a promise.
        var getMerged = merger('.jslintrc'),
    
            // Result objects to invoke callback with.
            results = [];
    
        async.each(files, function (file, callback) {
            fs.exists(file, function (exists) {
    
                // Future representation of the file's contents.
                var contentsPromise,
    
                    // Future representation of JSLINT options from .jslintrc files.
                    optionPromise;
    
                if (!exists) {
                    callback();
                    return;
                }
    
                contentsPromise = q.nfcall(fs.readFile, file, 'utf8');
                optionPromise = getMerged(path.dirname(file));
    
                // Parallelize IO operations.
                q.all([contentsPromise, optionPromise])
                    .spread(function (contents, option) {
                        var success = JSLINT(contents, option),
                            errors,
                            fileResults;
                        if (!success) {
                            errors = JSLINT.data().errors;
                            fileResults = errors.reduce(function (soFar, error) {
                                if (error === null) {
                                    return soFar;
                                }
                                return soFar.concat({
                                    file: file,
                                    error: error
                                });
                            }, []);
                            results = results.concat(fileResults);
                        }
                        process.nextTick(callback);
                    })
                    .catch(function (error) {
                        process.nextTick(function () {
                            callback(error);
                        });
                    })
                    .done();
            });
        }, function (error) {
            results = results.sort(function (a, b) {
                return a.file.charCodeAt(0) - b.file.charCodeAt(0);
            });
            callback(error, results);
        });
    }
    

    I want to do something potentially-blocking for each file. So async.each is the obvious choice. I can parallelize related operations per-iteration with q.all and reuse my option values if they apply to 2 or more files.

    Here, Async and Q each influence the control flow of the program, and Q represents values resolving to file contents sometime in the future. The libraries work well together. One does not need to "choose one over the other".

提交回复
热议问题