1.0.1 • Published 4 years ago

async-foreach-js v1.0.1

Weekly downloads
250
License
GPL-3.0
Repository
github
Last release
4 years ago

async-foreach-js

Array에 대한 forEach에서 Promise, Callback 함수 등을 포함한 비동기 함수를 순차적으로 실행하는 라이브러리입니다.

  • TypeScript 지원: ✅
npm install async-foreach-js

🇺🇸 English | US

사용 예

axios를 활용한 사용 예입니다.

require('async-foreach-js');
const axios = require('axios');

const crawlingTargets = [
    'https://apple.com',
    'https://amazon.com',
    'https://aws.amazon.com',
    'https://github.com',
    'https://npmjs.com'
];

let crawlingResults = [];

crawlingTargets.asyncForEach((element, next, escape) => (
    axios.get(element)
    .then((response) => {
        crawlingResults.push(response);
        next();
    })
    .catch((error) => {
        console.error(error);
        escape();
    })
))
.then(() => console.log('종료 됨!'))
.catch(() => console.error('오류로 인하여 루프가 종료되었습니다.'));

메서드

Array.prototype.asyncForEach((element: any, next: function[, escape: function, index: Number])

  • element 주어진 Array에서 현재 다루는 중에 있는 원소
  • next() 다음 원소를 호출하기 위한 함수
  • escape([message: any]) 루프를 중도 종료하기 위한 함수
  • index 주어진 Array에서 원소의 인덱스(번호)