项目作者: huang-x-h

项目描述 :
add utilities function to control async flow in jquery
高级语言: JavaScript
项目地址: git://github.com/huang-x-h/jquery.async.git
创建时间: 2016-07-21T11:10:59Z
项目社区:https://github.com/huang-x-h/jquery.async

开源协议:

下载


jquery.async

add utilities function to control async flow in jquery

Usage

  1. <script src="path/jquery.js"></script>
  2. <script src="path/jquery.async.js"></script>

API

$.series([deferreds], [initialValue])

Run deferreds collection of functions in series, each function consumes the return value of the previous function

  1. $.series(() => {
  2. return fetchUrl('defer1.json');
  3. }, (result) => {
  4. return { name: 'i am pure function' };
  5. }, (result) => {
  6. return fetchUrl('defer2.json');
  7. }).then((result) => {
  8. // do something
  9. });

$.parallel(...deferreds)

Run deferreds collection of functions in parallel, and return a promise that is fulfilled when all the items in the array are fulfilled

  1. $.parallel(() => {
  2. return fetchUrl('defer1.json');
  3. }, fetchUrl('defer2.json'), ['one', 'two']).then((v1, v2, v3) => {
  4. ...
  5. });

$.promisify(value)

Return a promise that will wrap the given value

  1. $.promisify(1).then(function(data) {
  2. console.log(data); // output: 1
  3. });

$.asyncEach(coll, iteratee)

Iterate over an array, with given iteratee function return 循环数组进行函数调用处理,返回各自调用结果

  1. $.asyncEach([1, 2, 3], function(item, coll) {
  2. var defer = $.Deferred();
  3. setTimeout(function() {
  4. defer.resolve(item + 1);
  5. }, 100);
  6. return defer.promise();
  7. }).then(function(data) {
  8. console.log(data); //output: 2, 3, 4
  9. });

$.polling(func, wait)

According to wait time polling implement the given func function

  1. var polling = $.polling(function() {
  2. console.log('polling');
  3. }, 1000);
  4. // start polling
  5. polling.start();
  6. // stop polling
  7. polling.stop();
  8. // polling times
  9. polling.times();

Changelog