Since ES7 would probably contain async/await pattern it would be nice to have a version on _.attempt that would work with it. For example
const response = await _.attemptAsync(makeRequest, requestOptions)
if (_(response).isError()) return console.error('Fail to request:', response)
const responseData = response.map(......)
In practice this means that _.attemptAsync returns a Promise that will never be rejected and resolve with either result or error
I understand that it may be a little bit early for such feature, but may be you may consider it for future versions
For async helpers I defer to async.
We'll keep an eye on the popularity of the request.
Ended up writing this locally for a project.
async function attemptAsync (func, ...args) {
try {
return await func(...args)
} catch(e) {
return e
}
}
// Usage
const dataOrError = await attemptAsync(async () => { ... })
@jdalton still not interested in adding this to lodash ?
Most helpful comment
Ended up writing this locally for a project.