Using ES2017 `async` functions with Async
masterAsync accepts native async functions. When using them, you do not pass a callback to the iteratee. Instead, you return a value or throw an error. Async will automatically handle the promise resolution/rejection.
Note: Async can only detect native async functions. For transpiled versions (e.g., via Babel), wrap them in async.asyncify().
async.mapLimit(files, 10, async file => {
const text = await util.promisify(fs.readFile)(dir + file, 'utf8')
const body = JSON.parse(text)
if (!(await checkValidity(body))) {
throw new Error(`${file} has invalid contents`)
}
return body
}, (err, contents) => {
if (err) throw err
console.log(contents)
})async.mapLimit(files, 10, async file => {
const text = await util.promisify(fs.readFile)(dir + file, 'utf8')
const body = JSON.parse(text) // <- a parse error here will be caught automatically
if (!(await checkValidity(body))) {
throw new Error(`${file} has invalid contents`) // <- this error will also be caught
}
return body // <- return a value!
}, (err, contents) => {
if (err) throw err
console.log(contents)
})