1.0.4 • Published 7 years ago

j-async v1.0.4

Weekly downloads
4
License
ISC
Repository
github
Last release
7 years ago

j-async

使用方法

使用NPM安装

npm install j-async --save-dev

引入j-async

// ES5
var asyncLimit = require('j-async')
// ES6
import asyncLimit from 'j-async'
const urls = [
  'http://placehold.it/1300x1600/E97452/fff',
  'http://placehold.it/1300x1300/4C6EB4/fff',
  'http://placehold.it/1300x1250/449F93/fff',
  'http://placehold.it/dsadsa/936FBC/fff',
  'http://placehold.it/1000x500/D25064/fff',
  'http://placehold.it/1300x1200/D25064/fff',
  'http://placehold.it/749x1327/D25064/fff'
]

// handler
const loadImg = (url) => {
  return new Promise((res, rej) => {
    const img = new Image()
    img.src = url
    img.onload = () => res(img)
    img.onerror = rej
  })
}

// promise
asyncLimit(
  urls,             // 请求的url数组
  loadImg,          // 请求的处理方法(要求返回Promise对象)
  4,                // 并发数限制值
  (data, err) => {  // 单个请求回调
    // ...
  }
)
.then(results => {  // 所有请求完成回调
  // ...
})

// async/await
;(async () => {
  const res = await asyncLimit(
    urls, 
    loadImg, 
    4,             
    (data, err) => {  
      // ...
    }
  )

  // ...
})()
1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

8 years ago

1.0.0

8 years ago