Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. Run Async
  2. =========
  3. [![npm](https://badge.fury.io/js/run-async.svg)](http://badge.fury.io/js/run-async) [![tests](https://travis-ci.org/SBoudrias/run-async.svg?branch=master)](http://travis-ci.org/SBoudrias/run-async) [![dependencies](https://david-dm.org/SBoudrias/run-async.svg?theme=shields.io)](https://david-dm.org/SBoudrias/run-async)
  4. Utility method to run a function either synchronously or asynchronously using a series of common patterns. This is useful for library author accepting sync or async functions as parameter. `runAsync` will always run them as an async method, and normalize the multiple signature.
  5. Installation
  6. =========
  7. ```bash
  8. npm install --save run-async
  9. ```
  10. Usage
  11. =========
  12. Here's a simple example print the function results and three options a user can provide a function.
  13. ```js
  14. var runAsync = require('run-async');
  15. var printAfter = function (func) {
  16. var cb = function (err, returnValue) {
  17. console.log(returnValue);
  18. };
  19. runAsync(func, cb)(/* arguments for func */);
  20. };
  21. ```
  22. #### Using `this.async`
  23. ```js
  24. printAfter(function () {
  25. var done = this.async();
  26. setTimeout(function () {
  27. done(null, 'done running with callback');
  28. }, 10);
  29. });
  30. ```
  31. #### Returning a promise
  32. ```js
  33. printAfter(function () {
  34. return new Promise(function (resolve, reject) {
  35. resolve('done running with promises');
  36. });
  37. });
  38. ```
  39. #### Synchronous function
  40. ```js
  41. printAfter(function () {
  42. return 'done running sync function';
  43. });
  44. ```
  45. ### runAsync.cb
  46. `runAsync.cb` supports all the function types that `runAsync` does and additionally a traditional **callback as the last argument** signature:
  47. ```js
  48. var runAsync = require('run-async');
  49. // IMPORTANT: The wrapped function must have a fixed number of parameters.
  50. runAsync.cb(function(a, b, cb) {
  51. cb(null, a + b);
  52. }, function(err, result) {
  53. console.log(result)
  54. })(1, 2)
  55. ```
  56. If your version of node support Promises natively (node >= 0.12), `runAsync` will return a promise. Example: `runAsync(func)(arg1, arg2).then(cb)`
  57. Licence
  58. ========
  59. Copyright (c) 2014 Simon Boudrias (twitter: @vaxilart)
  60. Licensed under the MIT license.