Guest User

Untitled

a guest
Jan 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. for(var i = 0; i < list.length; i++){
  2. mc_cli.get(list[i], function(err, response) {
  3. do_something(i);
  4. });
  5. }
  6.  
  7. do_something((function(x){return x})(i))
  8.  
  9. var create_closure = function(i) {
  10. return function() {
  11. return i;
  12. }
  13. }
  14.  
  15. do_something(create_closure(i)())
  16.  
  17. list.forEach(function(listItem, index){
  18. mc_cli.get(listItem, function(err, response) {
  19. do_something(index);
  20. });
  21. });
  22.  
  23. var total = parsed_result.list.length;
  24. var count = 0;
  25.  
  26. for(var i = 0; i < total; i++){
  27. (function(foo){
  28. mc_cli.get(parsed_result.list[foo], function(err, response) {
  29. do_something(foo);
  30. count++;
  31. if (count > total - 1) done();
  32. });
  33. }(i));
  34. }
  35.  
  36. // You can guarantee that this function will not be called until ALL of the
  37. // asynchronous functions have completed.
  38. function done() {
  39. console.log('All data has been loaded :).');
  40. }
  41.  
  42. function createCallback(i) {
  43. return function(){
  44. do_something(i);
  45. }
  46. }
  47.  
  48.  
  49. for(var i = 0; i < list.length; i++){
  50. mc_cli.get(list[i], createCallback(i));
  51. }
  52.  
  53. for(let i = 0; i < list.length; i++){
  54. mc_cli.get(list[i], function(err, response) {
  55. do_something(i);
  56. });
  57. }
  58.  
  59. var arr = ['Hello', 'World', 'Javascript', 'Async', ':)'];
  60. for( var i = 0; i < arr.length; i++) {
  61. (function(index){
  62. setTimeout(function(){
  63. console.log(arr[index]);
  64. }, 500);
Add Comment
Please, Sign In to add comment