Async Function Call: Call chained functions asynchronously

Recommend this page to a friend!
  Info   Example   Demos   View files Files   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 147 All time: 338 This week: 3Up
Version License JavaScript version Categories
async-function-call 1.0.1MIT/X Consortium ...5Language, Utilities and Tools
Description 

Author

This package can call chained functions asynchronously.

It adds a prototype for the function object so that a function can be called asynchronously.

Function calls may be chained, so when an asynchronous function succeeds or a given timeout period passed, it calls another asynchronous function call.

Innovation Award
JavaScript Programming Innovation award winner
July 2016
Winner
Sometimes it is necessary to call a sequence of functions but when these functions need to execute operations asynchronously you need to find a workaround so you the next functions is execute only after the last function asynchronous operations is finished.

This package implements a workaround by addong a new prototype to the function object, so you can chain the calls to multiple functions.

This ways when one asynchronous function succeeds or a given timeout period passed, it calls the next asynchronous function call.

Manuel Lemos
Picture of Andras Toth
  Performance   Level  
Name: Andras Toth <contact>
Classes: 22 packages by
Country: Hungary Hungary
Age: 51
All time rank: 31 in Hungary Hungary
Week rank: 1 Up
Innovation award
Innovation award
Nominee: 15x

Winner: 9x

Example

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>CallAsync Function</title> </head> <body> <div> Delayed start - 500 ms!<br><br> </div> <button id="btn">Start</button><br> <ul id="uln"></ul> <script> /* * callback: success function * error: error function * timeout: frunction start timeout * ...args arguments (arg1, arg2, etc) */ Function.prototype.callAsync = function(success, error, timeout, ...args) { var asyncFunctionCall = function(Self, args) { try { Self.apply(Self, args); if (typeof success == 'function') success(args); } catch (e) { if (typeof error == 'function') error(e); } } setTimeout(asyncFunctionCall.bind(null, this, args), !!timeout ? timeout : 0); return this; }; /* Example */ var runProcess = false; document.querySelector('button').addEventListener('click', function() { if (!runProcess) { runProcess = true; buildExample(); } else { this.textContent = 'Start'; console.log('Stopped!') runProcess = false; } }); function buildExample() { document.querySelector('#uln').innerHTML = ''; (function ge(selUL, selButton, func) { for (var i = 1; i <= 10000; i++) { func.callAsync(null, null, 0, document.querySelector(selButton), document.querySelector(selUL), i); } }).callAsync(function(args) { console.log(args) }, function(e) { console.log(e) }, 500, '#uln', '#btn', function(bt, ul, count) { if (runProcess) { var li = document.createElement('li'); bt.textContent = count; li.textContent = count + '-nth item.'; li.style.background = '#' + (Math.random() * 0xFFFFFF << 0).toString(16); ul.appendChild(li); } }); } /* Chaining example: function blabla (){ ... } blabla.callAsync(null, null, 500).callAsync(null, null, 1000).callAsync(null, null, 1500); */ </script> </body> </html>

Details

Javascript Async Function Call

Function prototype to call function in async mode.

Chainable async function call - success, error callbacks - configurable timeout

  • [Homepage] - Home page, demonstration

License

MIT

Author: Tóth András

http://atandrastoth.co.uk/

2015-06-10

[Homepage]:http://atandrastoth.co.uk/


  demoExternal page  
  Files folder image Files (3)  
File Role Description
Plain text file asyncFunctionCall.js Class Class source
Accessible without login Plain text file index.html Example Example
Accessible without login Plain text file README.md Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:147
This week:0
All time:338
This week:3Up