Comparing version 3.2.0 to 3.2.1
77
all.js
@@ -26,9 +26,74 @@ import createTester from './internal/createTester'; | ||
* | ||
* async.every(['file1','file2','file3'], function(filePath, callback) { | ||
* fs.access(filePath, function(err) { | ||
* callback(null, !err) | ||
* }); | ||
* }, function(err, result) { | ||
* // if result is true then every file exists | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* // dir4 does not exist | ||
* | ||
* const fileList = ['dir1/file1.txt','dir2/file3.txt','dir3/file5.txt']; | ||
* const withMissingFileList = ['file1.txt','file2.txt','file4.txt']; | ||
* | ||
* // asynchronous function that checks if a file exists | ||
* function fileExists(file, callback) { | ||
* fs.access(file, fs.constants.F_OK, (err) => { | ||
* callback(null, !err); | ||
* }); | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.every(fileList, fileExists, function(err, result) { | ||
* console.log(result); | ||
* // true | ||
* // result is true since every file exists | ||
* }); | ||
* | ||
* async.every(withMissingFileList, fileExists, function(err, result) { | ||
* console.log(result); | ||
* // false | ||
* // result is false since NOT every file exists | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.every(fileList, fileExists) | ||
* .then( result => { | ||
* console.log(result); | ||
* // true | ||
* // result is true since every file exists | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* async.every(withMissingFileList, fileExists) | ||
* .then( result => { | ||
* console.log(result); | ||
* // false | ||
* // result is false since NOT every file exists | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.every(fileList, fileExists); | ||
* console.log(result); | ||
* // true | ||
* // result is true since every file exists | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* async () => { | ||
* try { | ||
* let result = await async.every(withMissingFileList, fileExists); | ||
* console.log(result); | ||
* // false | ||
* // result is false since NOT every file exists | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -35,0 +100,0 @@ function every(coll, iteratee, callback) { |
78
any.js
@@ -28,9 +28,75 @@ import createTester from './internal/createTester'; | ||
* | ||
* async.some(['file1','file2','file3'], function(filePath, callback) { | ||
* fs.access(filePath, function(err) { | ||
* callback(null, !err) | ||
* }); | ||
* }, function(err, result) { | ||
* // if result is true then at least one of the files exists | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* // dir4 does not exist | ||
* | ||
* // asynchronous function that checks if a file exists | ||
* function fileExists(file, callback) { | ||
* fs.access(file, fs.constants.F_OK, (err) => { | ||
* callback(null, !err); | ||
* }); | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.some(['dir1/missing.txt','dir2/missing.txt','dir3/file5.txt'], fileExists, | ||
* function(err, result) { | ||
* console.log(result); | ||
* // true | ||
* // result is true since some file in the list exists | ||
* } | ||
*); | ||
* | ||
* async.some(['dir1/missing.txt','dir2/missing.txt','dir4/missing.txt'], fileExists, | ||
* function(err, result) { | ||
* console.log(result); | ||
* // false | ||
* // result is false since none of the files exists | ||
* } | ||
*); | ||
* | ||
* // Using Promises | ||
* async.some(['dir1/missing.txt','dir2/missing.txt','dir3/file5.txt'], fileExists) | ||
* .then( result => { | ||
* console.log(result); | ||
* // true | ||
* // result is true since some file in the list exists | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* async.some(['dir1/missing.txt','dir2/missing.txt','dir4/missing.txt'], fileExists) | ||
* .then( result => { | ||
* console.log(result); | ||
* // false | ||
* // result is false since none of the files exists | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.some(['dir1/missing.txt','dir2/missing.txt','dir3/file5.txt'], fileExists); | ||
* console.log(result); | ||
* // true | ||
* // result is true since some file in the list exists | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* async () => { | ||
* try { | ||
* let result = await async.some(['dir1/missing.txt','dir2/missing.txt','dir4/missing.txt'], fileExists); | ||
* console.log(result); | ||
* // false | ||
* // result is false since none of the files exists | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -37,0 +103,0 @@ function some(coll, iteratee, callback) { |
88
auto.js
@@ -47,11 +47,36 @@ import once from './internal/once'; | ||
* | ||
* //Using Callbacks | ||
* async.auto({ | ||
* // this function will just be passed a callback | ||
* readData: async.apply(fs.readFile, 'data.txt', 'utf-8'), | ||
* showData: ['readData', function(results, cb) { | ||
* // results.readData is the file's contents | ||
* // ... | ||
* get_data: function(callback) { | ||
* // async code to get some data | ||
* callback(null, 'data', 'converted to array'); | ||
* }, | ||
* make_folder: function(callback) { | ||
* // async code to create a directory to store a file in | ||
* // this is run at the same time as getting the data | ||
* callback(null, 'folder'); | ||
* }, | ||
* write_file: ['get_data', 'make_folder', function(results, callback) { | ||
* // once there is some data and the directory exists, | ||
* // write the data to a file in the directory | ||
* callback(null, 'filename'); | ||
* }], | ||
* email_link: ['write_file', function(results, callback) { | ||
* // once the file is written let's email a link to it... | ||
* callback(null, {'file':results.write_file, 'email':'user@example.com'}); | ||
* }] | ||
* }, callback); | ||
* }, function(err, results) { | ||
* if (err) { | ||
* console.log('err = ', err); | ||
* } | ||
* console.log('results = ', results); | ||
* // results = { | ||
* // get_data: ['data', 'converted to array'] | ||
* // make_folder; 'folder', | ||
* // write_file: 'filename' | ||
* // email_link: { file: 'filename', email: 'user@example.com' } | ||
* // } | ||
* }); | ||
* | ||
* //Using Promises | ||
* async.auto({ | ||
@@ -70,3 +95,2 @@ * get_data: function(callback) { | ||
* write_file: ['get_data', 'make_folder', function(results, callback) { | ||
* console.log('in write_file', JSON.stringify(results)); | ||
* // once there is some data and the directory exists, | ||
@@ -77,11 +101,53 @@ * // write the data to a file in the directory | ||
* email_link: ['write_file', function(results, callback) { | ||
* console.log('in email_link', JSON.stringify(results)); | ||
* // once the file is written let's email a link to it... | ||
* // results.write_file contains the filename returned by write_file. | ||
* callback(null, {'file':results.write_file, 'email':'user@example.com'}); | ||
* }] | ||
* }, function(err, results) { | ||
* }).then(results => { | ||
* console.log('results = ', results); | ||
* // results = { | ||
* // get_data: ['data', 'converted to array'] | ||
* // make_folder; 'folder', | ||
* // write_file: 'filename' | ||
* // email_link: { file: 'filename', email: 'user@example.com' } | ||
* // } | ||
* }).catch(err => { | ||
* console.log('err = ', err); | ||
* console.log('results = ', results); | ||
* }); | ||
* | ||
* //Using async/await | ||
* async () => { | ||
* try { | ||
* let results = await async.auto({ | ||
* get_data: function(callback) { | ||
* // async code to get some data | ||
* callback(null, 'data', 'converted to array'); | ||
* }, | ||
* make_folder: function(callback) { | ||
* // async code to create a directory to store a file in | ||
* // this is run at the same time as getting the data | ||
* callback(null, 'folder'); | ||
* }, | ||
* write_file: ['get_data', 'make_folder', function(results, callback) { | ||
* // once there is some data and the directory exists, | ||
* // write the data to a file in the directory | ||
* callback(null, 'filename'); | ||
* }], | ||
* email_link: ['write_file', function(results, callback) { | ||
* // once the file is written let's email a link to it... | ||
* callback(null, {'file':results.write_file, 'email':'user@example.com'}); | ||
* }] | ||
* }); | ||
* console.log('results = ', results); | ||
* // results = { | ||
* // get_data: ['data', 'converted to array'] | ||
* // make_folder; 'folder', | ||
* // write_file: 'filename' | ||
* // email_link: { file: 'filename', email: 'user@example.com' } | ||
* // } | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -88,0 +154,0 @@ export default function auto(tasks, concurrency, callback) { |
@@ -26,5 +26,73 @@ import concatLimit from './concatLimit'; | ||
* | ||
* async.concat(['dir1','dir2','dir3'], fs.readdir, function(err, files) { | ||
* // files is now a list of filenames that exist in the 3 directories | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* // dir4 does not exist | ||
* | ||
* let directoryList = ['dir1','dir2','dir3']; | ||
* let withMissingDirectoryList = ['dir1','dir2','dir3', 'dir4']; | ||
* | ||
* // Using callbacks | ||
* async.concat(directoryList, fs.readdir, function(err, results) { | ||
* if (err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(results); | ||
* // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ] | ||
* } | ||
* }); | ||
* | ||
* // Error Handling | ||
* async.concat(withMissingDirectoryList, fs.readdir, function(err, results) { | ||
* if (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* // since dir4 does not exist | ||
* } else { | ||
* console.log(results); | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.concat(directoryList, fs.readdir) | ||
* .then(results => { | ||
* console.log(results); | ||
* // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ] | ||
* }).catch(err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Error Handling | ||
* async.concat(withMissingDirectoryList, fs.readdir) | ||
* .then(results => { | ||
* console.log(results); | ||
* }).catch(err => { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* // since dir4 does not exist | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let results = await async.concat(directoryList, fs.readdir); | ||
* console.log(results); | ||
* // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ] | ||
* } catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* // Error Handling | ||
* async () => { | ||
* try { | ||
* let results = await async.concat(withMissingDirectoryList, fs.readdir); | ||
* console.log(results); | ||
* } catch (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* // since dir4 does not exist | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -31,0 +99,0 @@ function concat(coll, iteratee, callback) { |
@@ -33,9 +33,44 @@ import createTester from './internal/createTester'; | ||
* | ||
* async.detect(['file1','file2','file3'], function(filePath, callback) { | ||
* fs.access(filePath, function(err) { | ||
* callback(null, !err) | ||
* }); | ||
* }, function(err, result) { | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* | ||
* // asynchronous function that checks if a file exists | ||
* function fileExists(file, callback) { | ||
* fs.access(file, fs.constants.F_OK, (err) => { | ||
* callback(null, !err); | ||
* }); | ||
* } | ||
* | ||
* async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists, | ||
* function(err, result) { | ||
* console.log(result); | ||
* // dir1/file1.txt | ||
* // result now equals the first file in the list that exists | ||
* } | ||
*); | ||
* | ||
* // Using Promises | ||
* async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists) | ||
* .then(result => { | ||
* console.log(result); | ||
* // dir1/file1.txt | ||
* // result now equals the first file in the list that exists | ||
* }).catch(err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists); | ||
* console.log(result); | ||
* // dir1/file1.txt | ||
* // result now equals the file in the list that exists | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -42,0 +77,0 @@ function detect(coll, iteratee, callback) { |
89
each.js
@@ -32,33 +32,74 @@ import eachOf from './eachOf'; | ||
* | ||
* // assuming openFiles is an array of file names and saveFile is a function | ||
* // to save the modified contents of that file: | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* // dir4 does not exist | ||
* | ||
* async.each(openFiles, saveFile, function(err){ | ||
* // if any of the saves produced an error, err would equal that error | ||
* const fileList = [ 'dir1/file2.txt', 'dir2/file3.txt', 'dir/file5.txt']; | ||
* const withMissingFileList = ['dir1/file1.txt', 'dir4/file2.txt']; | ||
* | ||
* // asynchronous function that deletes a file | ||
* const deleteFile = function(file, callback) { | ||
* fs.unlink(file, callback); | ||
* }; | ||
* | ||
* // Using callbacks | ||
* async.each(fileList, deleteFile, function(err) { | ||
* if( err ) { | ||
* console.log(err); | ||
* } else { | ||
* console.log('All files have been deleted successfully'); | ||
* } | ||
* }); | ||
* | ||
* // assuming openFiles is an array of file names | ||
* async.each(openFiles, function(file, callback) { | ||
* // Error Handling | ||
* async.each(withMissingFileList, deleteFile, function(err){ | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* // since dir4/file2.txt does not exist | ||
* // dir1/file1.txt could have been deleted | ||
* }); | ||
* | ||
* // Perform operation on file here. | ||
* console.log('Processing file ' + file); | ||
* // Using Promises | ||
* async.each(fileList, deleteFile) | ||
* .then( () => { | ||
* console.log('All files have been deleted successfully'); | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* if( file.length > 32 ) { | ||
* console.log('This file name is too long'); | ||
* callback('File name too long'); | ||
* } else { | ||
* // Do work to process file here | ||
* console.log('File processed'); | ||
* callback(); | ||
* // Error Handling | ||
* async.each(fileList, deleteFile) | ||
* .then( () => { | ||
* console.log('All files have been deleted successfully'); | ||
* }).catch( err => { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* // since dir4/file2.txt does not exist | ||
* // dir1/file1.txt could have been deleted | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* await async.each(files, deleteFile); | ||
* } | ||
* }, function(err) { | ||
* // if any of the file processing produced an error, err would equal that error | ||
* if( err ) { | ||
* // One of the iterations produced an error. | ||
* // All processing will now stop. | ||
* console.log('A file failed to process'); | ||
* } else { | ||
* console.log('All files have been processed successfully'); | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* }); | ||
* } | ||
* | ||
* // Error Handling | ||
* async () => { | ||
* try { | ||
* await async.each(withMissingFileList, deleteFile); | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* // since dir4/file2.txt does not exist | ||
* // dir1/file1.txt could have been deleted | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -65,0 +106,0 @@ function eachLimit(coll, iteratee, callback) { |
@@ -63,8 +63,15 @@ import isArrayLike from './internal/isArrayLike'; | ||
* | ||
* var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"}; | ||
* var configs = {}; | ||
* // dev.json is a file containing a valid json object config for dev environment | ||
* // dev.json is a file containing a valid json object config for test environment | ||
* // prod.json is a file containing a valid json object config for prod environment | ||
* // invalid.json is a file with a malformed json object | ||
* | ||
* async.forEachOf(obj, function (value, key, callback) { | ||
* fs.readFile(__dirname + value, "utf8", function (err, data) { | ||
* if (err) return callback(err); | ||
* let configs = {}; //global variable | ||
* let validConfigFileMap = {dev: 'dev.json', test: 'test.json', prod: 'prod.json'}; | ||
* let invalidConfigFileMap = {dev: 'dev.json', test: 'test.json', invalid: 'invalid.json'}; | ||
* | ||
* // asynchronous function that reads a json file and parses the contents as json object | ||
* function parseFile(file, key, callback) { | ||
* fs.readFile(file, "utf8", function(err, data) { | ||
* if (err) return calback(err); | ||
* try { | ||
@@ -77,7 +84,69 @@ * configs[key] = JSON.parse(data); | ||
* }); | ||
* }, function (err) { | ||
* if (err) console.error(err.message); | ||
* // configs is now a map of JSON data | ||
* doSomethingWith(configs); | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.forEachOf(validConfigFileMap, parseFile, function (err) { | ||
* if (err) { | ||
* console.error(err); | ||
* } else { | ||
* console.log(configs); | ||
* // configs is now a map of JSON data, e.g. | ||
* // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json} | ||
* } | ||
* }); | ||
* | ||
* //Error handing | ||
* async.forEachOf(invalidConfigFileMap, parseFile, function (err) { | ||
* if (err) { | ||
* console.error(err); | ||
* // JSON parse error exception | ||
* } else { | ||
* console.log(configs); | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.forEachOf(validConfigFileMap, parseFile) | ||
* .then( () => { | ||
* console.log(configs); | ||
* // configs is now a map of JSON data, e.g. | ||
* // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json} | ||
* }).catch( err => { | ||
* console.error(err); | ||
* }); | ||
* | ||
* //Error handing | ||
* async.forEachOf(invalidConfigFileMap, parseFile) | ||
* .then( () => { | ||
* console.log(configs); | ||
* }).catch( err => { | ||
* console.error(err); | ||
* // JSON parse error exception | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.forEachOf(validConfigFileMap, parseFile); | ||
* console.log(configs); | ||
* // configs is now a map of JSON data, e.g. | ||
* // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json} | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* //Error handing | ||
* async () => { | ||
* try { | ||
* let result = await async.forEachOf(invalidConfigFileMap, parseFile); | ||
* console.log(configs); | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* // JSON parse error exception | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -84,0 +153,0 @@ function eachOf(coll, iteratee, callback) { |
77
every.js
@@ -26,9 +26,74 @@ import createTester from './internal/createTester'; | ||
* | ||
* async.every(['file1','file2','file3'], function(filePath, callback) { | ||
* fs.access(filePath, function(err) { | ||
* callback(null, !err) | ||
* }); | ||
* }, function(err, result) { | ||
* // if result is true then every file exists | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* // dir4 does not exist | ||
* | ||
* const fileList = ['dir1/file1.txt','dir2/file3.txt','dir3/file5.txt']; | ||
* const withMissingFileList = ['file1.txt','file2.txt','file4.txt']; | ||
* | ||
* // asynchronous function that checks if a file exists | ||
* function fileExists(file, callback) { | ||
* fs.access(file, fs.constants.F_OK, (err) => { | ||
* callback(null, !err); | ||
* }); | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.every(fileList, fileExists, function(err, result) { | ||
* console.log(result); | ||
* // true | ||
* // result is true since every file exists | ||
* }); | ||
* | ||
* async.every(withMissingFileList, fileExists, function(err, result) { | ||
* console.log(result); | ||
* // false | ||
* // result is false since NOT every file exists | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.every(fileList, fileExists) | ||
* .then( result => { | ||
* console.log(result); | ||
* // true | ||
* // result is true since every file exists | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* async.every(withMissingFileList, fileExists) | ||
* .then( result => { | ||
* console.log(result); | ||
* // false | ||
* // result is false since NOT every file exists | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.every(fileList, fileExists); | ||
* console.log(result); | ||
* // true | ||
* // result is true since every file exists | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* async () => { | ||
* try { | ||
* let result = await async.every(withMissingFileList, fileExists); | ||
* console.log(result); | ||
* // false | ||
* // result is false since NOT every file exists | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -35,0 +100,0 @@ function every(coll, iteratee, callback) { |
@@ -25,9 +25,49 @@ import _filter from './internal/filter'; | ||
* | ||
* async.filter(['file1','file2','file3'], function(filePath, callback) { | ||
* fs.access(filePath, function(err) { | ||
* callback(null, !err) | ||
* }); | ||
* }, function(err, results) { | ||
* // results now equals an array of the existing files | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* | ||
* const files = ['dir1/file1.txt','dir2/file3.txt','dir3/file6.txt']; | ||
* | ||
* // asynchronous function that checks if a file exists | ||
* function fileExists(file, callback) { | ||
* fs.access(file, fs.constants.F_OK, (err) => { | ||
* callback(null, !err); | ||
* }); | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.filter(files, fileExists, function(err, results) { | ||
* if(err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(results); | ||
* // [ 'dir1/file1.txt', 'dir2/file3.txt' ] | ||
* // results is now an array of the existing files | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.filter(files, fileExists) | ||
* .then(results => { | ||
* console.log(results); | ||
* // [ 'dir1/file1.txt', 'dir2/file3.txt' ] | ||
* // results is now an array of the existing files | ||
* }).catch(err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let results = await async.filter(files, fileExists); | ||
* console.log(results); | ||
* // [ 'dir1/file1.txt', 'dir2/file3.txt' ] | ||
* // results is now an array of the existing files | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -34,0 +74,0 @@ function filter (coll, iteratee, callback) { |
45
find.js
@@ -33,9 +33,44 @@ import createTester from './internal/createTester'; | ||
* | ||
* async.detect(['file1','file2','file3'], function(filePath, callback) { | ||
* fs.access(filePath, function(err) { | ||
* callback(null, !err) | ||
* }); | ||
* }, function(err, result) { | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* | ||
* // asynchronous function that checks if a file exists | ||
* function fileExists(file, callback) { | ||
* fs.access(file, fs.constants.F_OK, (err) => { | ||
* callback(null, !err); | ||
* }); | ||
* } | ||
* | ||
* async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists, | ||
* function(err, result) { | ||
* console.log(result); | ||
* // dir1/file1.txt | ||
* // result now equals the first file in the list that exists | ||
* } | ||
*); | ||
* | ||
* // Using Promises | ||
* async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists) | ||
* .then(result => { | ||
* console.log(result); | ||
* // dir1/file1.txt | ||
* // result now equals the first file in the list that exists | ||
* }).catch(err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.detect(['file3.txt','file2.txt','dir1/file1.txt'], fileExists); | ||
* console.log(result); | ||
* // dir1/file1.txt | ||
* // result now equals the file in the list that exists | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -42,0 +77,0 @@ function detect(coll, iteratee, callback) { |
@@ -26,5 +26,73 @@ import concatLimit from './concatLimit'; | ||
* | ||
* async.concat(['dir1','dir2','dir3'], fs.readdir, function(err, files) { | ||
* // files is now a list of filenames that exist in the 3 directories | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* // dir4 does not exist | ||
* | ||
* let directoryList = ['dir1','dir2','dir3']; | ||
* let withMissingDirectoryList = ['dir1','dir2','dir3', 'dir4']; | ||
* | ||
* // Using callbacks | ||
* async.concat(directoryList, fs.readdir, function(err, results) { | ||
* if (err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(results); | ||
* // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ] | ||
* } | ||
* }); | ||
* | ||
* // Error Handling | ||
* async.concat(withMissingDirectoryList, fs.readdir, function(err, results) { | ||
* if (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* // since dir4 does not exist | ||
* } else { | ||
* console.log(results); | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.concat(directoryList, fs.readdir) | ||
* .then(results => { | ||
* console.log(results); | ||
* // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ] | ||
* }).catch(err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Error Handling | ||
* async.concat(withMissingDirectoryList, fs.readdir) | ||
* .then(results => { | ||
* console.log(results); | ||
* }).catch(err => { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* // since dir4 does not exist | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let results = await async.concat(directoryList, fs.readdir); | ||
* console.log(results); | ||
* // [ 'file1.txt', 'file2.txt', 'file3.txt', 'file4.txt', file5.txt ] | ||
* } catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* // Error Handling | ||
* async () => { | ||
* try { | ||
* let results = await async.concat(withMissingDirectoryList, fs.readdir); | ||
* console.log(results); | ||
* } catch (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* // since dir4 does not exist | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -31,0 +99,0 @@ function concat(coll, iteratee, callback) { |
91
foldl.js
@@ -29,3 +29,3 @@ import eachOfSeries from './eachOfSeries'; | ||
* The `iteratee` should complete with the next state of the reduction. | ||
* If the iteratee complete with an error, the reduction is stopped and the | ||
* If the iteratee completes with an error, the reduction is stopped and the | ||
* main `callback` is immediately called with the error. | ||
@@ -39,10 +39,86 @@ * Invoked with (memo, item, callback). | ||
* | ||
* async.reduce([1,2,3], 0, function(memo, item, callback) { | ||
* // pointless async: | ||
* process.nextTick(function() { | ||
* callback(null, memo + item) | ||
* // file1.txt is a file that is 1000 bytes in size | ||
* // file2.txt is a file that is 2000 bytes in size | ||
* // file3.txt is a file that is 3000 bytes in size | ||
* // file4.txt does not exist | ||
* | ||
* const fileList = ['file1.txt','file2.txt','file3.txt']; | ||
* const withMissingFileList = ['file1.txt','file2.txt','file3.txt', 'file4.txt']; | ||
* | ||
* // asynchronous function that computes the file size in bytes | ||
* // file size is added to the memoized value, then returned | ||
* function getFileSizeInBytes(memo, file, callback) { | ||
* fs.stat(file, function(err, stat) { | ||
* if (err) { | ||
* return callback(err); | ||
* } | ||
* callback(null, memo + stat.size); | ||
* }); | ||
* }, function(err, result) { | ||
* // result is now equal to the last value of memo, which is 6 | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.reduce(fileList, 0, getFileSizeInBytes, function(err, result) { | ||
* if (err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(result); | ||
* // 6000 | ||
* // which is the sum of the file sizes of the three files | ||
* } | ||
* }); | ||
* | ||
* // Error Handling | ||
* async.reduce(withMissingFileList, 0, getFileSizeInBytes, function(err, result) { | ||
* if (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* } else { | ||
* console.log(result); | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.reduce(fileList, 0, getFileSizeInBytes) | ||
* .then( result => { | ||
* console.log(result); | ||
* // 6000 | ||
* // which is the sum of the file sizes of the three files | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Error Handling | ||
* async.reduce(withMissingFileList, 0, getFileSizeInBytes) | ||
* .then( result => { | ||
* console.log(result); | ||
* }).catch( err => { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.reduce(fileList, 0, getFileSizeInBytes); | ||
* console.log(result); | ||
* // 6000 | ||
* // which is the sum of the file sizes of the three files | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* // Error Handling | ||
* async () => { | ||
* try { | ||
* let result = await async.reduce(withMissingFileList, 0, getFileSizeInBytes); | ||
* console.log(result); | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -60,2 +136,1 @@ function reduce(coll, memo, iteratee, callback) { | ||
export default awaitify(reduce, 4) | ||
@@ -18,3 +18,3 @@ import reduce from './reduce'; | ||
* The `iteratee` should complete with the next state of the reduction. | ||
* If the iteratee complete with an error, the reduction is stopped and the | ||
* If the iteratee completes with an error, the reduction is stopped and the | ||
* main `callback` is immediately called with the error. | ||
@@ -21,0 +21,0 @@ * Invoked with (memo, item, callback). |
@@ -32,33 +32,74 @@ import eachOf from './eachOf'; | ||
* | ||
* // assuming openFiles is an array of file names and saveFile is a function | ||
* // to save the modified contents of that file: | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* // dir4 does not exist | ||
* | ||
* async.each(openFiles, saveFile, function(err){ | ||
* // if any of the saves produced an error, err would equal that error | ||
* const fileList = [ 'dir1/file2.txt', 'dir2/file3.txt', 'dir/file5.txt']; | ||
* const withMissingFileList = ['dir1/file1.txt', 'dir4/file2.txt']; | ||
* | ||
* // asynchronous function that deletes a file | ||
* const deleteFile = function(file, callback) { | ||
* fs.unlink(file, callback); | ||
* }; | ||
* | ||
* // Using callbacks | ||
* async.each(fileList, deleteFile, function(err) { | ||
* if( err ) { | ||
* console.log(err); | ||
* } else { | ||
* console.log('All files have been deleted successfully'); | ||
* } | ||
* }); | ||
* | ||
* // assuming openFiles is an array of file names | ||
* async.each(openFiles, function(file, callback) { | ||
* // Error Handling | ||
* async.each(withMissingFileList, deleteFile, function(err){ | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* // since dir4/file2.txt does not exist | ||
* // dir1/file1.txt could have been deleted | ||
* }); | ||
* | ||
* // Perform operation on file here. | ||
* console.log('Processing file ' + file); | ||
* // Using Promises | ||
* async.each(fileList, deleteFile) | ||
* .then( () => { | ||
* console.log('All files have been deleted successfully'); | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* if( file.length > 32 ) { | ||
* console.log('This file name is too long'); | ||
* callback('File name too long'); | ||
* } else { | ||
* // Do work to process file here | ||
* console.log('File processed'); | ||
* callback(); | ||
* // Error Handling | ||
* async.each(fileList, deleteFile) | ||
* .then( () => { | ||
* console.log('All files have been deleted successfully'); | ||
* }).catch( err => { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* // since dir4/file2.txt does not exist | ||
* // dir1/file1.txt could have been deleted | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* await async.each(files, deleteFile); | ||
* } | ||
* }, function(err) { | ||
* // if any of the file processing produced an error, err would equal that error | ||
* if( err ) { | ||
* // One of the iterations produced an error. | ||
* // All processing will now stop. | ||
* console.log('A file failed to process'); | ||
* } else { | ||
* console.log('All files have been processed successfully'); | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* }); | ||
* } | ||
* | ||
* // Error Handling | ||
* async () => { | ||
* try { | ||
* await async.each(withMissingFileList, deleteFile); | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* // since dir4/file2.txt does not exist | ||
* // dir1/file1.txt could have been deleted | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -65,0 +106,0 @@ function eachLimit(coll, iteratee, callback) { |
@@ -63,8 +63,15 @@ import isArrayLike from './internal/isArrayLike'; | ||
* | ||
* var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"}; | ||
* var configs = {}; | ||
* // dev.json is a file containing a valid json object config for dev environment | ||
* // dev.json is a file containing a valid json object config for test environment | ||
* // prod.json is a file containing a valid json object config for prod environment | ||
* // invalid.json is a file with a malformed json object | ||
* | ||
* async.forEachOf(obj, function (value, key, callback) { | ||
* fs.readFile(__dirname + value, "utf8", function (err, data) { | ||
* if (err) return callback(err); | ||
* let configs = {}; //global variable | ||
* let validConfigFileMap = {dev: 'dev.json', test: 'test.json', prod: 'prod.json'}; | ||
* let invalidConfigFileMap = {dev: 'dev.json', test: 'test.json', invalid: 'invalid.json'}; | ||
* | ||
* // asynchronous function that reads a json file and parses the contents as json object | ||
* function parseFile(file, key, callback) { | ||
* fs.readFile(file, "utf8", function(err, data) { | ||
* if (err) return calback(err); | ||
* try { | ||
@@ -77,7 +84,69 @@ * configs[key] = JSON.parse(data); | ||
* }); | ||
* }, function (err) { | ||
* if (err) console.error(err.message); | ||
* // configs is now a map of JSON data | ||
* doSomethingWith(configs); | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.forEachOf(validConfigFileMap, parseFile, function (err) { | ||
* if (err) { | ||
* console.error(err); | ||
* } else { | ||
* console.log(configs); | ||
* // configs is now a map of JSON data, e.g. | ||
* // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json} | ||
* } | ||
* }); | ||
* | ||
* //Error handing | ||
* async.forEachOf(invalidConfigFileMap, parseFile, function (err) { | ||
* if (err) { | ||
* console.error(err); | ||
* // JSON parse error exception | ||
* } else { | ||
* console.log(configs); | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.forEachOf(validConfigFileMap, parseFile) | ||
* .then( () => { | ||
* console.log(configs); | ||
* // configs is now a map of JSON data, e.g. | ||
* // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json} | ||
* }).catch( err => { | ||
* console.error(err); | ||
* }); | ||
* | ||
* //Error handing | ||
* async.forEachOf(invalidConfigFileMap, parseFile) | ||
* .then( () => { | ||
* console.log(configs); | ||
* }).catch( err => { | ||
* console.error(err); | ||
* // JSON parse error exception | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.forEachOf(validConfigFileMap, parseFile); | ||
* console.log(configs); | ||
* // configs is now a map of JSON data, e.g. | ||
* // { dev: //parsed dev.json, test: //parsed test.json, prod: //parsed prod.json} | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* //Error handing | ||
* async () => { | ||
* try { | ||
* let result = await async.forEachOf(invalidConfigFileMap, parseFile); | ||
* console.log(configs); | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* // JSON parse error exception | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -84,0 +153,0 @@ function eachOf(coll, iteratee, callback) { |
@@ -30,11 +30,65 @@ import groupByLimit from './groupByLimit'; | ||
* | ||
* async.groupBy(['userId1', 'userId2', 'userId3'], function(userId, callback) { | ||
* db.findById(userId, function(err, user) { | ||
* if (err) return callback(err); | ||
* return callback(null, user.age); | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* // dir4 does not exist | ||
* | ||
* const files = ['dir1/file1.txt','dir2','dir4'] | ||
* | ||
* // asynchronous function that detects file type as none, file, or directory | ||
* function detectFile(file, callback) { | ||
* fs.stat(file, function(err, stat) { | ||
* if (err) { | ||
* return callback(null, 'none'); | ||
* } | ||
* callback(null, stat.isDirectory() ? 'directory' : 'file'); | ||
* }); | ||
* }, function(err, result) { | ||
* // result is object containing the userIds grouped by age | ||
* // e.g. { 30: ['userId1', 'userId3'], 42: ['userId2']}; | ||
* } | ||
* | ||
* //Using callbacks | ||
* async.groupBy(files, detectFile, function(err, result) { | ||
* if(err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(result); | ||
* // { | ||
* // file: [ 'dir1/file1.txt' ], | ||
* // none: [ 'dir4' ], | ||
* // directory: [ 'dir2'] | ||
* // } | ||
* // result is object containing the files grouped by type | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.groupBy(files, detectFile) | ||
* .then( result => { | ||
* console.log(result); | ||
* // { | ||
* // file: [ 'dir1/file1.txt' ], | ||
* // none: [ 'dir4' ], | ||
* // directory: [ 'dir2'] | ||
* // } | ||
* // result is object containing the files grouped by type | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.groupBy(files, detectFile); | ||
* console.log(result); | ||
* // { | ||
* // file: [ 'dir1/file1.txt' ], | ||
* // none: [ 'dir4' ], | ||
* // directory: [ 'dir2'] | ||
* // } | ||
* // result is object containing the files grouped by type | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -41,0 +95,0 @@ export default function groupBy (coll, iteratee, callback) { |
@@ -18,3 +18,3 @@ import groupByLimit from './groupByLimit'; | ||
* @param {Function} [callback] - A callback which is called when all `iteratee` | ||
* functions have finished, or an error occurs. Result is an `Object` whoses | ||
* functions have finished, or an error occurs. Result is an `Object` whose | ||
* properties are arrays of values which returned the corresponding key. | ||
@@ -21,0 +21,0 @@ * @returns {Promise} a promise, if no callback is passed |
@@ -29,3 +29,3 @@ import eachOfSeries from './eachOfSeries'; | ||
* The `iteratee` should complete with the next state of the reduction. | ||
* If the iteratee complete with an error, the reduction is stopped and the | ||
* If the iteratee completes with an error, the reduction is stopped and the | ||
* main `callback` is immediately called with the error. | ||
@@ -39,10 +39,86 @@ * Invoked with (memo, item, callback). | ||
* | ||
* async.reduce([1,2,3], 0, function(memo, item, callback) { | ||
* // pointless async: | ||
* process.nextTick(function() { | ||
* callback(null, memo + item) | ||
* // file1.txt is a file that is 1000 bytes in size | ||
* // file2.txt is a file that is 2000 bytes in size | ||
* // file3.txt is a file that is 3000 bytes in size | ||
* // file4.txt does not exist | ||
* | ||
* const fileList = ['file1.txt','file2.txt','file3.txt']; | ||
* const withMissingFileList = ['file1.txt','file2.txt','file3.txt', 'file4.txt']; | ||
* | ||
* // asynchronous function that computes the file size in bytes | ||
* // file size is added to the memoized value, then returned | ||
* function getFileSizeInBytes(memo, file, callback) { | ||
* fs.stat(file, function(err, stat) { | ||
* if (err) { | ||
* return callback(err); | ||
* } | ||
* callback(null, memo + stat.size); | ||
* }); | ||
* }, function(err, result) { | ||
* // result is now equal to the last value of memo, which is 6 | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.reduce(fileList, 0, getFileSizeInBytes, function(err, result) { | ||
* if (err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(result); | ||
* // 6000 | ||
* // which is the sum of the file sizes of the three files | ||
* } | ||
* }); | ||
* | ||
* // Error Handling | ||
* async.reduce(withMissingFileList, 0, getFileSizeInBytes, function(err, result) { | ||
* if (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* } else { | ||
* console.log(result); | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.reduce(fileList, 0, getFileSizeInBytes) | ||
* .then( result => { | ||
* console.log(result); | ||
* // 6000 | ||
* // which is the sum of the file sizes of the three files | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Error Handling | ||
* async.reduce(withMissingFileList, 0, getFileSizeInBytes) | ||
* .then( result => { | ||
* console.log(result); | ||
* }).catch( err => { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.reduce(fileList, 0, getFileSizeInBytes); | ||
* console.log(result); | ||
* // 6000 | ||
* // which is the sum of the file sizes of the three files | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* // Error Handling | ||
* async () => { | ||
* try { | ||
* let result = await async.reduce(withMissingFileList, 0, getFileSizeInBytes); | ||
* console.log(result); | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -60,2 +136,1 @@ function reduce(coll, memo, iteratee, callback) { | ||
export default awaitify(reduce, 4) | ||
@@ -5,8 +5,11 @@ import wrapAsync from './wrapAsync'; | ||
return (fn, ...args) => wrapAsync(fn)(...args, (err, ...resultArgs) => { | ||
/* istanbul ignore else */ | ||
if (typeof console === 'object') { | ||
/* istanbul ignore else */ | ||
if (err) { | ||
/* istanbul ignore else */ | ||
if (console.error) { | ||
console.error(err); | ||
} | ||
} else if (console[name]) { | ||
} else if (console[name]) { /* istanbul ignore else */ | ||
resultArgs.forEach(x => console[name](x)); | ||
@@ -13,0 +16,0 @@ } |
'use strict'; | ||
/* istanbul ignore file */ | ||
export var hasQueueMicrotask = typeof queueMicrotask === 'function' && queueMicrotask; | ||
export var hasSetImmediate = typeof setImmediate === 'function' && setImmediate; | ||
@@ -17,3 +18,5 @@ export var hasNextTick = typeof process === 'object' && typeof process.nextTick === 'function'; | ||
if (hasSetImmediate) { | ||
if (hasQueueMicrotask) { | ||
_defer = queueMicrotask; | ||
} else if (hasSetImmediate) { | ||
_defer = setImmediate; | ||
@@ -20,0 +23,0 @@ } else if (hasNextTick) { |
84
map.js
@@ -38,5 +38,85 @@ import _map from './internal/map'; | ||
* | ||
* async.map(['file1','file2','file3'], fs.stat, function(err, results) { | ||
* // results is now an array of stats for each file | ||
* // file1.txt is a file that is 1000 bytes in size | ||
* // file2.txt is a file that is 2000 bytes in size | ||
* // file3.txt is a file that is 3000 bytes in size | ||
* // file4.txt does not exist | ||
* | ||
* const fileList = ['file1.txt','file2.txt','file3.txt']; | ||
* const withMissingFileList = ['file1.txt','file2.txt','file4.txt']; | ||
* | ||
* // asynchronous function that returns the file size in bytes | ||
* function getFileSizeInBytes(file, callback) { | ||
* fs.stat(file, function(err, stat) { | ||
* if (err) { | ||
* return callback(err); | ||
* } | ||
* callback(null, stat.size); | ||
* }); | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.map(fileList, getFileSizeInBytes, function(err, results) { | ||
* if (err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(results); | ||
* // results is now an array of the file size in bytes for each file, e.g. | ||
* // [ 1000, 2000, 3000] | ||
* } | ||
* }); | ||
* | ||
* // Error Handling | ||
* async.map(withMissingFileList, getFileSizeInBytes, function(err, results) { | ||
* if (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* } else { | ||
* console.log(results); | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.map(fileList, getFileSizeInBytes) | ||
* .then( results => { | ||
* console.log(results); | ||
* // results is now an array of the file size in bytes for each file, e.g. | ||
* // [ 1000, 2000, 3000] | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Error Handling | ||
* async.map(withMissingFileList, getFileSizeInBytes) | ||
* .then( results => { | ||
* console.log(results); | ||
* }).catch( err => { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let results = await async.map(fileList, getFileSizeInBytes); | ||
* console.log(results); | ||
* // results is now an array of the file size in bytes for each file, e.g. | ||
* // [ 1000, 2000, 3000] | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* // Error Handling | ||
* async () => { | ||
* try { | ||
* let results = await async.map(withMissingFileList, getFileSizeInBytes); | ||
* console.log(results); | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -43,0 +123,0 @@ function map (coll, iteratee, callback) { |
112
mapValues.js
@@ -33,16 +33,106 @@ import mapValuesLimit from './mapValuesLimit'; | ||
* | ||
* async.mapValues({ | ||
* f1: 'file1', | ||
* f2: 'file2', | ||
* f3: 'file3' | ||
* }, function (file, key, callback) { | ||
* fs.stat(file, callback); | ||
* }, function(err, result) { | ||
* // result is now a map of stats for each file, e.g. | ||
* // file1.txt is a file that is 1000 bytes in size | ||
* // file2.txt is a file that is 2000 bytes in size | ||
* // file3.txt is a file that is 3000 bytes in size | ||
* // file4.txt does not exist | ||
* | ||
* const fileMap = { | ||
* f1: 'file1.txt', | ||
* f2: 'file2.txt', | ||
* f3: 'file3.txt' | ||
* }; | ||
* | ||
* const withMissingFileMap = { | ||
* f1: 'file1.txt', | ||
* f2: 'file2.txt', | ||
* f3: 'file4.txt' | ||
* }; | ||
* | ||
* // asynchronous function that returns the file size in bytes | ||
* function getFileSizeInBytes(file, key, callback) { | ||
* fs.stat(file, function(err, stat) { | ||
* if (err) { | ||
* return callback(err); | ||
* } | ||
* callback(null, stat.size); | ||
* }); | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.mapValues(fileMap, getFileSizeInBytes, function(err, result) { | ||
* if (err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(result); | ||
* // result is now a map of file size in bytes for each file, e.g. | ||
* // { | ||
* // f1: 1000, | ||
* // f2: 2000, | ||
* // f3: 3000 | ||
* // } | ||
* } | ||
* }); | ||
* | ||
* // Error handling | ||
* async.mapValues(withMissingFileMap, getFileSizeInBytes, function(err, result) { | ||
* if (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* } else { | ||
* console.log(result); | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.mapValues(fileMap, getFileSizeInBytes) | ||
* .then( result => { | ||
* console.log(result); | ||
* // result is now a map of file size in bytes for each file, e.g. | ||
* // { | ||
* // f1: [stats for file1], | ||
* // f2: [stats for file2], | ||
* // f3: [stats for file3] | ||
* // f1: 1000, | ||
* // f2: 2000, | ||
* // f3: 3000 | ||
* // } | ||
* }).catch (err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Error Handling | ||
* async.mapValues(withMissingFileMap, getFileSizeInBytes) | ||
* .then( result => { | ||
* console.log(result); | ||
* }).catch (err => { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.mapValues(fileMap, getFileSizeInBytes); | ||
* console.log(result); | ||
* // result is now a map of file size in bytes for each file, e.g. | ||
* // { | ||
* // f1: 1000, | ||
* // f2: 2000, | ||
* // f3: 3000 | ||
* // } | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* // Error Handling | ||
* async () => { | ||
* try { | ||
* let result = await async.mapValues(withMissingFileMap, getFileSizeInBytes); | ||
* console.log(result); | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -49,0 +139,0 @@ export default function mapValues(obj, iteratee, callback) { |
{ | ||
"name": "async-es", | ||
"description": "Higher-order functions and common patterns for asynchronous code", | ||
"version": "3.2.0", | ||
"version": "3.2.1", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "author": "Caolan McMahon", |
@@ -40,2 +40,4 @@ import eachOf from './eachOf'; | ||
* @example | ||
* | ||
* //Using Callbacks | ||
* async.parallel([ | ||
@@ -52,6 +54,5 @@ * function(callback) { | ||
* } | ||
* ], | ||
* // optional callback | ||
* function(err, results) { | ||
* // the results array will equal ['one','two'] even though | ||
* ], function(err, results) { | ||
* console.log(results); | ||
* // results is equal to ['one','two'] even though | ||
* // the second function had a shorter timeout. | ||
@@ -73,4 +74,92 @@ * }); | ||
* }, function(err, results) { | ||
* // results is now equals to: {one: 1, two: 2} | ||
* console.log(results); | ||
* // results is equal to: { one: 1, two: 2 } | ||
* }); | ||
* | ||
* //Using Promises | ||
* async.parallel([ | ||
* function(callback) { | ||
* setTimeout(function() { | ||
* callback(null, 'one'); | ||
* }, 200); | ||
* }, | ||
* function(callback) { | ||
* setTimeout(function() { | ||
* callback(null, 'two'); | ||
* }, 100); | ||
* } | ||
* ]).then(results => { | ||
* console.log(results); | ||
* // results is equal to ['one','two'] even though | ||
* // the second function had a shorter timeout. | ||
* }).catch(err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // an example using an object instead of an array | ||
* async.parallel({ | ||
* one: function(callback) { | ||
* setTimeout(function() { | ||
* callback(null, 1); | ||
* }, 200); | ||
* }, | ||
* two: function(callback) { | ||
* setTimeout(function() { | ||
* callback(null, 2); | ||
* }, 100); | ||
* } | ||
* }).then(results => { | ||
* console.log(results); | ||
* // results is equal to: { one: 1, two: 2 } | ||
* }).catch(err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* //Using async/await | ||
* async () => { | ||
* try { | ||
* let results = await async.parallel([ | ||
* function(callback) { | ||
* setTimeout(function() { | ||
* callback(null, 'one'); | ||
* }, 200); | ||
* }, | ||
* function(callback) { | ||
* setTimeout(function() { | ||
* callback(null, 'two'); | ||
* }, 100); | ||
* } | ||
* ]); | ||
* console.log(results); | ||
* // results is equal to ['one','two'] even though | ||
* // the second function had a shorter timeout. | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* // an example using an object instead of an array | ||
* async () => { | ||
* try { | ||
* let results = await async.parallel({ | ||
* one: function(callback) { | ||
* setTimeout(function() { | ||
* callback(null, 1); | ||
* }, 200); | ||
* }, | ||
* two: function(callback) { | ||
* setTimeout(function() { | ||
* callback(null, 2); | ||
* }, 100); | ||
* } | ||
* }); | ||
* console.log(results); | ||
* // results is equal to: { one: 1, two: 2 } | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -77,0 +166,0 @@ export default function parallel(tasks, callback) { |
@@ -31,2 +31,3 @@ import setImmediate from './setImmediate'; | ||
var q = queue(worker, concurrency); | ||
var processingScheduled = false; | ||
@@ -59,3 +60,9 @@ q._tasks = new Heap(); | ||
setImmediate(q.process); | ||
if (!processingScheduled) { | ||
processingScheduled = true; | ||
setImmediate(() => { | ||
processingScheduled = false; | ||
q.process(); | ||
}); | ||
} | ||
}; | ||
@@ -62,0 +69,0 @@ |
@@ -32,3 +32,3 @@ import queue from './internal/queue'; | ||
* a promise that rejects if an error occurs. | ||
* @property {AsyncFunction} unshirtAsync - the same as `q.unshift`, except this returns | ||
* @property {AsyncFunction} unshiftAsync - the same as `q.unshift`, except this returns | ||
* a promise that rejects if an error occurs. | ||
@@ -72,3 +72,3 @@ * @property {Function} remove - remove items from the queue that match a test | ||
* @example | ||
* const q = aync.queue(worker, 2) | ||
* const q = async.queue(worker, 2) | ||
* q.push(item1) | ||
@@ -75,0 +75,0 @@ * q.push(item2) |
@@ -29,3 +29,3 @@ import eachOfSeries from './eachOfSeries'; | ||
* The `iteratee` should complete with the next state of the reduction. | ||
* If the iteratee complete with an error, the reduction is stopped and the | ||
* If the iteratee completes with an error, the reduction is stopped and the | ||
* main `callback` is immediately called with the error. | ||
@@ -39,10 +39,86 @@ * Invoked with (memo, item, callback). | ||
* | ||
* async.reduce([1,2,3], 0, function(memo, item, callback) { | ||
* // pointless async: | ||
* process.nextTick(function() { | ||
* callback(null, memo + item) | ||
* // file1.txt is a file that is 1000 bytes in size | ||
* // file2.txt is a file that is 2000 bytes in size | ||
* // file3.txt is a file that is 3000 bytes in size | ||
* // file4.txt does not exist | ||
* | ||
* const fileList = ['file1.txt','file2.txt','file3.txt']; | ||
* const withMissingFileList = ['file1.txt','file2.txt','file3.txt', 'file4.txt']; | ||
* | ||
* // asynchronous function that computes the file size in bytes | ||
* // file size is added to the memoized value, then returned | ||
* function getFileSizeInBytes(memo, file, callback) { | ||
* fs.stat(file, function(err, stat) { | ||
* if (err) { | ||
* return callback(err); | ||
* } | ||
* callback(null, memo + stat.size); | ||
* }); | ||
* }, function(err, result) { | ||
* // result is now equal to the last value of memo, which is 6 | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.reduce(fileList, 0, getFileSizeInBytes, function(err, result) { | ||
* if (err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(result); | ||
* // 6000 | ||
* // which is the sum of the file sizes of the three files | ||
* } | ||
* }); | ||
* | ||
* // Error Handling | ||
* async.reduce(withMissingFileList, 0, getFileSizeInBytes, function(err, result) { | ||
* if (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* } else { | ||
* console.log(result); | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.reduce(fileList, 0, getFileSizeInBytes) | ||
* .then( result => { | ||
* console.log(result); | ||
* // 6000 | ||
* // which is the sum of the file sizes of the three files | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Error Handling | ||
* async.reduce(withMissingFileList, 0, getFileSizeInBytes) | ||
* .then( result => { | ||
* console.log(result); | ||
* }).catch( err => { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.reduce(fileList, 0, getFileSizeInBytes); | ||
* console.log(result); | ||
* // 6000 | ||
* // which is the sum of the file sizes of the three files | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* // Error Handling | ||
* async () => { | ||
* try { | ||
* let result = await async.reduce(withMissingFileList, 0, getFileSizeInBytes); | ||
* console.log(result); | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -60,2 +136,1 @@ function reduce(coll, memo, iteratee, callback) { | ||
export default awaitify(reduce, 4) | ||
@@ -18,3 +18,3 @@ import reduce from './reduce'; | ||
* The `iteratee` should complete with the next state of the reduction. | ||
* If the iteratee complete with an error, the reduction is stopped and the | ||
* If the iteratee completes with an error, the reduction is stopped and the | ||
* main `callback` is immediately called with the error. | ||
@@ -21,0 +21,0 @@ * Invoked with (memo, item, callback). |
@@ -24,10 +24,44 @@ import _reject from './internal/reject'; | ||
* | ||
* async.reject(['file1','file2','file3'], function(filePath, callback) { | ||
* fs.access(filePath, function(err) { | ||
* callback(null, !err) | ||
* }); | ||
* }, function(err, results) { | ||
* // results now equals an array of missing files | ||
* createFiles(results); | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* | ||
* const fileList = ['dir1/file1.txt','dir2/file3.txt','dir3/file6.txt']; | ||
* | ||
* // asynchronous function that checks if a file exists | ||
* function fileExists(file, callback) { | ||
* fs.access(file, fs.constants.F_OK, (err) => { | ||
* callback(null, !err); | ||
* }); | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.reject(fileList, fileExists, function(err, results) { | ||
* // [ 'dir3/file6.txt' ] | ||
* // results now equals an array of the non-existing files | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.reject(fileList, fileExists) | ||
* .then( results => { | ||
* console.log(results); | ||
* // [ 'dir3/file6.txt' ] | ||
* // results now equals an array of the non-existing files | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let results = await async.reject(fileList, fileExists); | ||
* console.log(results); | ||
* // [ 'dir3/file6.txt' ] | ||
* // results now equals an array of the non-existing files | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -34,0 +68,0 @@ function reject (coll, iteratee, callback) { |
@@ -25,9 +25,49 @@ import _filter from './internal/filter'; | ||
* | ||
* async.filter(['file1','file2','file3'], function(filePath, callback) { | ||
* fs.access(filePath, function(err) { | ||
* callback(null, !err) | ||
* }); | ||
* }, function(err, results) { | ||
* // results now equals an array of the existing files | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* | ||
* const files = ['dir1/file1.txt','dir2/file3.txt','dir3/file6.txt']; | ||
* | ||
* // asynchronous function that checks if a file exists | ||
* function fileExists(file, callback) { | ||
* fs.access(file, fs.constants.F_OK, (err) => { | ||
* callback(null, !err); | ||
* }); | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.filter(files, fileExists, function(err, results) { | ||
* if(err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(results); | ||
* // [ 'dir1/file1.txt', 'dir2/file3.txt' ] | ||
* // results is now an array of the existing files | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.filter(files, fileExists) | ||
* .then(results => { | ||
* console.log(results); | ||
* // [ 'dir1/file1.txt', 'dir2/file3.txt' ] | ||
* // results is now an array of the existing files | ||
* }).catch(err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let results = await async.filter(files, fileExists); | ||
* console.log(results); | ||
* // [ 'dir1/file1.txt', 'dir2/file3.txt' ] | ||
* // results is now an array of the existing files | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -34,0 +74,0 @@ function filter (coll, iteratee, callback) { |
120
series.js
@@ -39,25 +39,33 @@ import _parallel from './internal/parallel'; | ||
* @example | ||
* | ||
* //Using Callbacks | ||
* async.series([ | ||
* function(callback) { | ||
* // do some stuff ... | ||
* callback(null, 'one'); | ||
* setTimeout(function() { | ||
* // do some async task | ||
* callback(null, 'one'); | ||
* }, 200); | ||
* }, | ||
* function(callback) { | ||
* // do some more stuff ... | ||
* callback(null, 'two'); | ||
* setTimeout(function() { | ||
* // then do another async task | ||
* callback(null, 'two'); | ||
* }, 100); | ||
* } | ||
* ], | ||
* // optional callback | ||
* function(err, results) { | ||
* // results is now equal to ['one', 'two'] | ||
* ], function(err, results) { | ||
* console.log(results); | ||
* // results is equal to ['one','two'] | ||
* }); | ||
* | ||
* // an example using objects instead of arrays | ||
* async.series({ | ||
* one: function(callback) { | ||
* setTimeout(function() { | ||
* // do some async task | ||
* callback(null, 1); | ||
* }, 200); | ||
* }, | ||
* two: function(callback){ | ||
* two: function(callback) { | ||
* setTimeout(function() { | ||
* // then do another async task | ||
* callback(null, 2); | ||
@@ -67,4 +75,96 @@ * }, 100); | ||
* }, function(err, results) { | ||
* // results is now equal to: {one: 1, two: 2} | ||
* console.log(results); | ||
* // results is equal to: { one: 1, two: 2 } | ||
* }); | ||
* | ||
* //Using Promises | ||
* async.series([ | ||
* function(callback) { | ||
* setTimeout(function() { | ||
* callback(null, 'one'); | ||
* }, 200); | ||
* }, | ||
* function(callback) { | ||
* setTimeout(function() { | ||
* callback(null, 'two'); | ||
* }, 100); | ||
* } | ||
* ]).then(results => { | ||
* console.log(results); | ||
* // results is equal to ['one','two'] | ||
* }).catch(err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // an example using an object instead of an array | ||
* async.series({ | ||
* one: function(callback) { | ||
* setTimeout(function() { | ||
* // do some async task | ||
* callback(null, 1); | ||
* }, 200); | ||
* }, | ||
* two: function(callback) { | ||
* setTimeout(function() { | ||
* // then do another async task | ||
* callback(null, 2); | ||
* }, 100); | ||
* } | ||
* }).then(results => { | ||
* console.log(results); | ||
* // results is equal to: { one: 1, two: 2 } | ||
* }).catch(err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* //Using async/await | ||
* async () => { | ||
* try { | ||
* let results = await async.series([ | ||
* function(callback) { | ||
* setTimeout(function() { | ||
* // do some async task | ||
* callback(null, 'one'); | ||
* }, 200); | ||
* }, | ||
* function(callback) { | ||
* setTimeout(function() { | ||
* // then do another async task | ||
* callback(null, 'two'); | ||
* }, 100); | ||
* } | ||
* ]); | ||
* console.log(results); | ||
* // results is equal to ['one','two'] | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* // an example using an object instead of an array | ||
* async () => { | ||
* try { | ||
* let results = await async.parallel({ | ||
* one: function(callback) { | ||
* setTimeout(function() { | ||
* // do some async task | ||
* callback(null, 1); | ||
* }, 200); | ||
* }, | ||
* two: function(callback) { | ||
* setTimeout(function() { | ||
* // then do another async task | ||
* callback(null, 2); | ||
* }, 100); | ||
* } | ||
* }); | ||
* console.log(results); | ||
* // results is equal to: { one: 1, two: 2 } | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -71,0 +171,0 @@ export default function series(tasks, callback) { |
78
some.js
@@ -28,9 +28,75 @@ import createTester from './internal/createTester'; | ||
* | ||
* async.some(['file1','file2','file3'], function(filePath, callback) { | ||
* fs.access(filePath, function(err) { | ||
* callback(null, !err) | ||
* }); | ||
* }, function(err, result) { | ||
* // if result is true then at least one of the files exists | ||
* // dir1 is a directory that contains file1.txt, file2.txt | ||
* // dir2 is a directory that contains file3.txt, file4.txt | ||
* // dir3 is a directory that contains file5.txt | ||
* // dir4 does not exist | ||
* | ||
* // asynchronous function that checks if a file exists | ||
* function fileExists(file, callback) { | ||
* fs.access(file, fs.constants.F_OK, (err) => { | ||
* callback(null, !err); | ||
* }); | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.some(['dir1/missing.txt','dir2/missing.txt','dir3/file5.txt'], fileExists, | ||
* function(err, result) { | ||
* console.log(result); | ||
* // true | ||
* // result is true since some file in the list exists | ||
* } | ||
*); | ||
* | ||
* async.some(['dir1/missing.txt','dir2/missing.txt','dir4/missing.txt'], fileExists, | ||
* function(err, result) { | ||
* console.log(result); | ||
* // false | ||
* // result is false since none of the files exists | ||
* } | ||
*); | ||
* | ||
* // Using Promises | ||
* async.some(['dir1/missing.txt','dir2/missing.txt','dir3/file5.txt'], fileExists) | ||
* .then( result => { | ||
* console.log(result); | ||
* // true | ||
* // result is true since some file in the list exists | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* async.some(['dir1/missing.txt','dir2/missing.txt','dir4/missing.txt'], fileExists) | ||
* .then( result => { | ||
* console.log(result); | ||
* // false | ||
* // result is false since none of the files exists | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.some(['dir1/missing.txt','dir2/missing.txt','dir3/file5.txt'], fileExists); | ||
* console.log(result); | ||
* // true | ||
* // result is true since some file in the list exists | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
* async () => { | ||
* try { | ||
* let result = await async.some(['dir1/missing.txt','dir2/missing.txt','dir4/missing.txt'], fileExists); | ||
* console.log(result); | ||
* // false | ||
* // result is false since none of the files exists | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -37,0 +103,0 @@ function some(coll, iteratee, callback) { |
134
sortBy.js
@@ -27,11 +27,30 @@ import map from './map'; | ||
* | ||
* async.sortBy(['file1','file2','file3'], function(file, callback) { | ||
* fs.stat(file, function(err, stats) { | ||
* callback(err, stats.mtime); | ||
* // bigfile.txt is a file that is 251100 bytes in size | ||
* // mediumfile.txt is a file that is 11000 bytes in size | ||
* // smallfile.txt is a file that is 121 bytes in size | ||
* | ||
* // asynchronous function that returns the file size in bytes | ||
* function getFileSizeInBytes(file, callback) { | ||
* fs.stat(file, function(err, stat) { | ||
* if (err) { | ||
* return callback(err); | ||
* } | ||
* callback(null, stat.size); | ||
* }); | ||
* }, function(err, results) { | ||
* // results is now the original array of files sorted by | ||
* // modified date | ||
* }); | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.sortBy(['mediumfile.txt','smallfile.txt','bigfile.txt'], getFileSizeInBytes, | ||
* function(err, results) { | ||
* if (err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(results); | ||
* // results is now the original array of files sorted by | ||
* // file size (ascending by default), e.g. | ||
* // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt'] | ||
* } | ||
* } | ||
* ); | ||
* | ||
* // By modifying the callback parameter the | ||
@@ -41,14 +60,97 @@ * // sorting order can be influenced: | ||
* // ascending order | ||
* async.sortBy([1,9,3,5], function(x, callback) { | ||
* callback(null, x); | ||
* }, function(err,result) { | ||
* // result callback | ||
* }); | ||
* async.sortBy(['mediumfile.txt','smallfile.txt','bigfile.txt'], function(file, callback) { | ||
* getFileSizeInBytes(file, function(getFileSizeErr, fileSize) { | ||
* if (getFileSizeErr) return callback(getFileSizeErr); | ||
* callback(null, fileSize); | ||
* }); | ||
* }, function(err, results) { | ||
* if (err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(results); | ||
* // results is now the original array of files sorted by | ||
* // file size (ascending by default), e.g. | ||
* // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt'] | ||
* } | ||
* } | ||
* ); | ||
* | ||
* // descending order | ||
* async.sortBy([1,9,3,5], function(x, callback) { | ||
* callback(null, x*-1); //<- x*-1 instead of x, turns the order around | ||
* }, function(err,result) { | ||
* // result callback | ||
* async.sortBy(['bigfile.txt','mediumfile.txt','smallfile.txt'], function(file, callback) { | ||
* getFileSizeInBytes(file, function(getFileSizeErr, fileSize) { | ||
* if (getFileSizeErr) { | ||
* return callback(getFileSizeErr); | ||
* } | ||
* callback(null, fileSize * -1); | ||
* }); | ||
* }, function(err, results) { | ||
* if (err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(results); | ||
* // results is now the original array of files sorted by | ||
* // file size (ascending by default), e.g. | ||
* // [ 'bigfile.txt', 'mediumfile.txt', 'smallfile.txt'] | ||
* } | ||
* } | ||
* ); | ||
* | ||
* // Error handling | ||
* async.sortBy(['mediumfile.txt','smallfile.txt','missingfile.txt'], getFileSizeInBytes, | ||
* function(err, results) { | ||
* if (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* } else { | ||
* console.log(results); | ||
* } | ||
* } | ||
* ); | ||
* | ||
* // Using Promises | ||
* async.sortBy(['mediumfile.txt','smallfile.txt','bigfile.txt'], getFileSizeInBytes) | ||
* .then( results => { | ||
* console.log(results); | ||
* // results is now the original array of files sorted by | ||
* // file size (ascending by default), e.g. | ||
* // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt'] | ||
* }).catch( err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Error handling | ||
* async.sortBy(['mediumfile.txt','smallfile.txt','missingfile.txt'], getFileSizeInBytes) | ||
* .then( results => { | ||
* console.log(results); | ||
* }).catch( err => { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* }); | ||
* | ||
* // Using async/await | ||
* (async () => { | ||
* try { | ||
* let results = await async.sortBy(['bigfile.txt','mediumfile.txt','smallfile.txt'], getFileSizeInBytes); | ||
* console.log(results); | ||
* // results is now the original array of files sorted by | ||
* // file size (ascending by default), e.g. | ||
* // [ 'smallfile.txt', 'mediumfile.txt', 'bigfile.txt'] | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* })(); | ||
* | ||
* // Error handling | ||
* async () => { | ||
* try { | ||
* let results = await async.sortBy(['missingfile.txt','mediumfile.txt','smallfile.txt'], getFileSizeInBytes); | ||
* console.log(results); | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* // [ Error: ENOENT: no such file or directory ] | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -55,0 +157,0 @@ function sortBy (coll, iteratee, callback) { |
122
transform.js
@@ -28,22 +28,114 @@ import eachOf from './eachOf'; | ||
* | ||
* async.transform([1,2,3], function(acc, item, index, callback) { | ||
* // pointless async: | ||
* process.nextTick(function() { | ||
* acc[index] = item * 2 | ||
* callback(null) | ||
* // file1.txt is a file that is 1000 bytes in size | ||
* // file2.txt is a file that is 2000 bytes in size | ||
* // file3.txt is a file that is 3000 bytes in size | ||
* | ||
* // helper function that returns human-readable size format from bytes | ||
* function formatBytes(bytes, decimals = 2) { | ||
* // implementation not included for brevity | ||
* return humanReadbleFilesize; | ||
* } | ||
* | ||
* const fileList = ['file1.txt','file2.txt','file3.txt']; | ||
* | ||
* // asynchronous function that returns the file size, transformed to human-readable format | ||
* // e.g. 1024 bytes = 1KB, 1234 bytes = 1.21 KB, 1048576 bytes = 1MB, etc. | ||
* function transformFileSize(acc, value, key, callback) { | ||
* fs.stat(value, function(err, stat) { | ||
* if (err) { | ||
* return callback(err); | ||
* } | ||
* acc[key] = formatBytes(stat.size); | ||
* callback(null); | ||
* }); | ||
* }, function(err, result) { | ||
* // result is now equal to [2, 4, 6] | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.transform(fileList, transformFileSize, function(err, result) { | ||
* if(err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(result); | ||
* // [ '1000 Bytes', '1.95 KB', '2.93 KB' ] | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.transform(fileList, transformFileSize) | ||
* .then(result => { | ||
* console.log(result); | ||
* // [ '1000 Bytes', '1.95 KB', '2.93 KB' ] | ||
* }).catch(err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Using async/await | ||
* (async () => { | ||
* try { | ||
* let result = await async.transform(fileList, transformFileSize); | ||
* console.log(result); | ||
* // [ '1000 Bytes', '1.95 KB', '2.93 KB' ] | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* })(); | ||
* | ||
* @example | ||
* | ||
* async.transform({a: 1, b: 2, c: 3}, function (obj, val, key, callback) { | ||
* setImmediate(function () { | ||
* obj[key] = val * 2; | ||
* callback(); | ||
* }) | ||
* }, function (err, result) { | ||
* // result is equal to {a: 2, b: 4, c: 6} | ||
* }) | ||
* // file1.txt is a file that is 1000 bytes in size | ||
* // file2.txt is a file that is 2000 bytes in size | ||
* // file3.txt is a file that is 3000 bytes in size | ||
* | ||
* // helper function that returns human-readable size format from bytes | ||
* function formatBytes(bytes, decimals = 2) { | ||
* // implementation not included for brevity | ||
* return humanReadbleFilesize; | ||
* } | ||
* | ||
* const fileMap = { f1: 'file1.txt', f2: 'file2.txt', f3: 'file3.txt' }; | ||
* | ||
* // asynchronous function that returns the file size, transformed to human-readable format | ||
* // e.g. 1024 bytes = 1KB, 1234 bytes = 1.21 KB, 1048576 bytes = 1MB, etc. | ||
* function transformFileSize(acc, value, key, callback) { | ||
* fs.stat(value, function(err, stat) { | ||
* if (err) { | ||
* return callback(err); | ||
* } | ||
* acc[key] = formatBytes(stat.size); | ||
* callback(null); | ||
* }); | ||
* } | ||
* | ||
* // Using callbacks | ||
* async.transform(fileMap, transformFileSize, function(err, result) { | ||
* if(err) { | ||
* console.log(err); | ||
* } else { | ||
* console.log(result); | ||
* // { f1: '1000 Bytes', f2: '1.95 KB', f3: '2.93 KB' } | ||
* } | ||
* }); | ||
* | ||
* // Using Promises | ||
* async.transform(fileMap, transformFileSize) | ||
* .then(result => { | ||
* console.log(result); | ||
* // { f1: '1000 Bytes', f2: '1.95 KB', f3: '2.93 KB' } | ||
* }).catch(err => { | ||
* console.log(err); | ||
* }); | ||
* | ||
* // Using async/await | ||
* async () => { | ||
* try { | ||
* let result = await async.transform(fileMap, transformFileSize); | ||
* console.log(result); | ||
* // { f1: '1000 Bytes', f2: '1.95 KB', f3: '2.93 KB' } | ||
* } | ||
* catch (err) { | ||
* console.log(err); | ||
* } | ||
* } | ||
* | ||
*/ | ||
@@ -50,0 +142,0 @@ export default function transform (coll, accumulator, iteratee, callback) { |
@@ -30,3 +30,3 @@ import whilst from './whilst'; | ||
* let finished = false | ||
* async.until(function test(page, cb) { | ||
* async.until(function test(cb) { | ||
* cb(null, finished) | ||
@@ -33,0 +33,0 @@ * }, function iter(next) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
311093
7678