项目作者: vokeio

项目描述 :
promise-tool
高级语言: JavaScript
项目地址: git://github.com/vokeio/promise-tool.git
创建时间: 2016-12-28T17:43:20Z
项目社区:https://github.com/vokeio/promise-tool

开源协议:Mozilla Public License 2.0

下载


Promise Tool

A promised library of tools for Node.js and the browser.

Install

npm install promise-tool --save

API

  • PromiseTool.lift() Lifts a callback style function or prototype object and converts to a promise/s first argument must be an error.

  • PromiseTool.series A given task will not be started until the preceding task completes.

    • tasks The array of functions to execute in series.
      • task A function which returns a promise.
        • resolve(result) A result which will be appended to the end of each task/function as parameter.
    • parameters An Array of parameters to be passed to each task/function. Optional
  • PromiseTool.setTimeout

    • delay The number of milliseconds to wait before calling resolve.
    • ...args Optional arguments to pass when the resolve is executed.
  • PromiseTool.setInterval
    • delay The number of milliseconds to wait before calling resolve.
    • method A function that repeats on each interval. This function will fire upon each interval unless one of the following returns are implemented.
      • Return Value Actions
        • result Any valid JavaScript error type. Will fire the reject and pass the error.
        • result A boolean that calls resolve if true or reject if false.
        • result Any thing returned besides null, undefined, false, and a valid Error type will resolve with that return value as the first argument.
        • result Both are ignored and will not trigger the resolve or reject.
    • ...args Optional arguments to pass when the resolve is executed.
  • PromiseTool.setImmediate
    • ...args Optional arguments to pass when the resolve is executed.
  • PromiseTool.clearTimeout
    • timeout A Timeout object as returned by setInterval().
  • PromiseTool.clearInterval
    • interval A Interval object as returned by setInterval().
  • PromiseTool.clearImmediate
    • immediate An Immediate object as returned by setImmediate().

Examples

Series

  1. var PromiseTool = require('../index.js');
  2. function a (one, two) {
  3. return new Promise (function (resolve) {
  4. setTimeout(function () {
  5. return resolve(`a-${one}${two}`);
  6. }, 900);
  7. });
  8. }
  9. function b (one, two, result) {
  10. return new Promise (function (resolve) {
  11. setTimeout(function () {
  12. result = `${result} b-${one}${two}`;
  13. return resolve(result);
  14. }, 600);
  15. });
  16. }
  17. PromiseTool.series([a, b], [1, 2]).then(function (result) {
  18. console.log(result); // a-12 b-12
  19. });

Timers

  1. var PromiseTool = require('promise-timers');
  2. var delay = 500;
  3. PromiseTool.setTimeout(delay).then(function (args) {
  4. // this refers to timeout
  5. console.log(args);
  6. console.log('timeout done');
  7. });
  8. var i = 0;
  9. function method () {
  10. // this refers to interval
  11. if (i > 5) {
  12. return true;
  13. } else {
  14. console.log(i);
  15. i++;
  16. }
  17. };
  18. PromiseTool.setInterval(delay, method).then(function (args) {
  19. // this refers to interval
  20. console.log(args);
  21. console.log('interval done');
  22. });
  23. PromiseTool.setImmediate().then(function (args) {
  24. // this refers to immediate
  25. console.log(args);
  26. console.log('immediate done');
  27. });

Authors

AlexanderElias

License

Why You Should Choose MPL-2.0
This project is licensed under the MPL-2.0 License