demo1.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var Queue = require('../');
  2. var q = Queue.Promise; //a Promise utils;
  3. //Realize a queue with a maximum concurrency of 1
  4. var queue1 = new Queue(1,{
  5. "retry":0 //Number of retries
  6. ,"retryIsJump":false //retry now?
  7. ,"timeout":0 //The timeout period
  8. });
  9. //a return promise function
  10. function testfn(i){
  11. return new Promise(function(resolve,reject){
  12. setTimeout(function(){
  13. resolve(i)
  14. },300)
  15. })
  16. }
  17. var log = function(msg){ console.log(msg); }
  18. queue1.push(testfn,[1]) //add job (FIFO)
  19. .then(log);
  20. queue1.push(function(){return 2;}) //The normal function returns a promise according to the Promise / A + rule
  21. .then(log);
  22. queue1.unshift(testfn,[0]) //add job (LIFO)
  23. .then(log);
  24. queue1.addLikeArray([3,4],testfn,{'workResolve':log}) //Add multiple jobs with Array, Work done will execute 'workResolve'
  25. .then(log)
  26. queue1.addLikeProps({'a':5,'b':6,'c':7},testfn,{'workResolve':log}) //Add multiple jobs with Map,
  27. .then(log)
  28. queue1.add(function(resolve,reject){
  29. resolve(8)
  30. }).then(log)
  31. queue1.go(testfn,['go']).then(log)