async.js

比如each方法,依次完成

  1. async.each(openFiles, saveFile, function(err){
  2. // if any of the saves produced an error, err would equal that error
  3. });

比如瀑布式的

  1. async.waterfall([
  2. function(callback) {
  3. callback(null, 'one', 'two');
  4. },
  5. function(arg1, arg2, callback) {
  6. // arg1 now equals 'one' and arg2 now equals 'two'
  7. callback(null, 'three');
  8. },
  9. function(arg1, callback) {
  10. // arg1 now equals 'three'
  11. callback(null, 'done');
  12. }
  13. ], function (err, result) {
  14. // result now equals 'done'
  15. });
  16. // Or, with named functions:
  17. async.waterfall([
  18. myFirstFunction,
  19. mySecondFunction,
  20. myLastFunction,
  21. ], function (err, result) {
  22. // result now equals 'done'
  23. });
  24. function myFirstFunction(callback) {
  25. callback(null, 'one', 'two');
  26. }
  27. function mySecondFunction(arg1, arg2, callback) {
  28. // arg1 now equals 'one' and arg2 now equals 'two'
  29. callback(null, 'three');
  30. }
  31. function myLastFunction(arg1, callback) {
  32. // arg1 now equals 'three'
  33. callback(null, 'done');
  34. }

比如对各种api或数据结构的扩展。

总之async.js是个不错的库,但比过于独立,和promise/a+规范不一样,也就是说我们需要多学aysnc.js,又不能和promise/a+一起用

所以,不如直接上promise/a+。