Skip to content

Instantly share code, notes, and snippets.

@gagan-bansal
Last active June 17, 2023 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gagan-bansal/398a7c087de4e9d9360fc7e09fbc8ffe to your computer and use it in GitHub Desktop.
Save gagan-bansal/398a7c087de4e9d9360fc7e09fbc8ffe to your computer and use it in GitHub Desktop.
async tryEach example
const {tryEach} = require('async');
(async () => {
const array = [12, 6 , 30, 55, 5];
const testArr = array.map(v => {
return async function test() {
console.log('val: ', v);
if (v > 20) return Promise.resolve(v);
else return Promise.reject(new Error('not found'));
}
});
const found = await tryEach(testArr);
console.log('found: ', found);
})();
const {tryEach} = require('async');
(async () => {
const array = [12, 6 , 30, 55, 5];
const testArr = array.map(v => {
return function test(cb) {
console.log('val: ', v);
if (v > 20) cb(null, v);
else cb(new Error('not found'));
}
});
const found = await tryEach(testArr);
console.log('found: ', found);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment