项目作者: DawnImpulse

项目描述 :
Helps synchronizing each async loop call of an array (without promise).
高级语言: JavaScript
项目地址: git://github.com/DawnImpulse/wait-loop.git
创建时间: 2018-02-24T03:07:53Z
项目社区:https://github.com/DawnImpulse/wait-loop

开源协议:ISC License

下载


wait-loop

NPM
Build Status GitHub issues
npm version npm

wait-loop helps to synchronize each async loop call for an array . wait-loop works on ‘deasync’ nodejs event loop blocking mechanism.

A problem arises when we wish to iterate an array and perform an async task on each element but we wish to call next
iteration only after first one is finished working.

wait-loop helps in this regard by using node events & deasync event blocking to achieve a synchronous work.

Note : The following module is helpful for those who are yet to work with bluebird promises as .each method in bluebird is good solution for this problem.

To achieve our goal we will use following function -

  • each function

    1. each(array,task)
    • array - is the array of elements on which our loop will work
    • task - the function inside which user performs the async task ,there will be two params provided by it.
      • element - element (which is the current array element)
      • position - the current index (you can avoid it if you don’t want it)
    • next() function is used to switch to next iteration , if there is an error call next(err)
    • on(func) function will create an active listener which will fire after either all iterations are completed or an error occurred.
      • func(error) - it is a function as parameter inside on
        • error - if the loop succeeds the error will be undefined else it will contain the error

    Note : Although the on is internally using node events but you can start listening to it before or after each function. This is due to the internal module(wait-loop) handling which will only starts the work after the listener (on) is attached.

Example -

  1. var loop = require('wait-loop');
  2. loop.each(array, function(elem) {
  3. //an async call to read a file
  4. require('fs').readFile(elem, 'UTF-8', (err, resp) => {
  5. //call next(err) if there is an error
  6. if (err)
  7. loop.next(err);
  8. else
  9. loop.next();
  10. }); //end of read file
  11. }); //end of loop.each
  12. loop.on(err => {
  13. //our error should be undefined for successful completion
  14. if (err)
  15. //there is an error
  16. else
  17. //successfully executed
  18. });

Versions

  • v1.0.0

    • Initial release - containing the basic each function only.

Upcoming

  • A new complete synchronous function without event listener utilizing try/catch

Special Thanks

  • Deasync

    Pull requests are always welcomed (kindly sign commits with GPG keys. THANKS)

Contact

License (ISC)

  1. ISC Licence
  2. Copyright 2018 Saksham (DawnImpulse)
  3. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
  4. provided that the above copyright notice and this permission notice appear in all copies.
  5. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  6. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  7. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  8. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  9. OR PERFORMANCE OF THIS SOFTWARE.