underscore: async each loop waiting "inside"

inselberg

How can i wait for callbacks inside a loop changing a variable, and still using asynchronicity?

The 2nd example is using async, in this case i don't know how to add a 2nd parameter sum to wait, in order to avoid a global var sum. Called like wait(sum,value); with a return value sum

wait is a representation for a complex function, which i use in my real-problem, so it can't be rewritten into "inline"-code and has to stay "function".

Example1:

var _ = require('underscore');
var arr = [1,2,3,4,5,6,7];
var sum = 0;

function wait(item,callback) {
  setTimeout(function() {
    callback(item);
  }, Math.ceil(Math.random()*1000));
}

var done = _.after(arr.length,function(value) {
  console.log('sum ='+value);
})

_.each(arr,function(itm) {
  wait(itm,function(value) {
    console.log('waiting... '+value);
    sum = sum + value;
  })
 // Please wait for the callback
  console.log(itm);
  done(sum);
});

Example2:

function asyncExample2() {
var async = require('async');
var arr = [1,2,3,4,5,6,7];

function otherWait(item, callback) {
    setTimeout(function() {
    callback(item); // call this when you're done with whatever you're doing
  }, Math.ceil(Math.random()*1000));
}

function wait(item, callback) {
  setTimeout(function() {
    otherWait(item,function() {
    console.log(item);
  });
  callback(item); 
  }, Math.ceil(Math.random()*1000));
}

function done() { console.log("sum = "+sum);};

var sum = 0;
async.forEach(arr, wait, done);
}

Desired Call:

sum = wait(sum,item) 
Herrington Darkholme

The easiest way to do this is putting done in the function wait. It makes done called only after the last callback is executed.

var arr = [1,2,3,4,5,6,7];
var sum = 0;

function wait(item,callback) {
  setTimeout(function() {
    callback(item);
    done(sum);
  }, Math.ceil(Math.random()*1000));
}

var done = _.after(arr.length,function(value) {
  console.log('sum ='+value);
})

_.each(arr,function(itm) {
  wait(itm,function(value) {
    console.log('waiting... '+value);
    sum = sum + value;
  })
  // Please wait for the callback
  console.log(itm);
  //done(sum);
});

输出:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

$ q许诺与Underscore _each

来自分类Dev

jQuery for循环,而不是underscore.js _.each

来自分类Dev

Threads: Busy Waiting - Empty While-Loop

来自分类Dev

Javascript for each loop

来自分类Dev

async.each和async.eachSeries之间的区别

来自分类Dev

async.each嵌套在async.waterfall中

来自分类Dev

结合async.each和async.series

来自分类Dev

async.each 和 async.every 的区别?

来自分类Dev

Continue at first loop , inside the second loop

来自分类Dev

Using an IF statement inside loop vs outside the loop

来自分类Dev

JS:async.each的承诺是什么?

来自分类Dev

使用async.each获取对象密钥

来自分类Dev

更新async.each中的请求参数?

来自分类Dev

async.each - 什么是回调?

来自分类Dev

Best way for SqlConnection inside a loop

来自分类Dev

Is it possible to put XCTAsserts inside a loop?

来自分类Dev

Sequentially executing promises inside a for loop

来自分类Dev

find command behaviour inside for loop

来自分类Dev

async.compose函数和underscore.compose函数有什么区别?

来自分类Dev

如何在Node JS FS模块中使用Typescript Async / waiting with promise

来自分类Dev

如何在async.each内部具有async.waterfall的外部函数中调用async.each

来自分类Dev

我如何在underscore.js的_.each循环的每次迭代内添加延迟?

来自分类Dev

如何使用_.reduce(和_.each)从Underscore.js重写_.every / _。all

来自分类Dev

如何使用_.reduce(和_.each)从Underscore.js重写_.every / _。all

来自分类Dev

我如何在underscore.js的_.each循环的每次迭代中添加延迟?

来自分类Dev

Underscore.js _.each函数的第三个参数如何工作?

来自分类Dev

Expression matching each parameter inside delimited block

来自分类Dev

Jpa persist/merge throws ConcurrentModificationException inside loop

来自分类Dev

Don't loop code inside foreach bootstrap

Related 相关文章

  1. 1

    $ q许诺与Underscore _each

  2. 2

    jQuery for循环,而不是underscore.js _.each

  3. 3

    Threads: Busy Waiting - Empty While-Loop

  4. 4

    Javascript for each loop

  5. 5

    async.each和async.eachSeries之间的区别

  6. 6

    async.each嵌套在async.waterfall中

  7. 7

    结合async.each和async.series

  8. 8

    async.each 和 async.every 的区别?

  9. 9

    Continue at first loop , inside the second loop

  10. 10

    Using an IF statement inside loop vs outside the loop

  11. 11

    JS:async.each的承诺是什么?

  12. 12

    使用async.each获取对象密钥

  13. 13

    更新async.each中的请求参数?

  14. 14

    async.each - 什么是回调?

  15. 15

    Best way for SqlConnection inside a loop

  16. 16

    Is it possible to put XCTAsserts inside a loop?

  17. 17

    Sequentially executing promises inside a for loop

  18. 18

    find command behaviour inside for loop

  19. 19

    async.compose函数和underscore.compose函数有什么区别?

  20. 20

    如何在Node JS FS模块中使用Typescript Async / waiting with promise

  21. 21

    如何在async.each内部具有async.waterfall的外部函数中调用async.each

  22. 22

    我如何在underscore.js的_.each循环的每次迭代内添加延迟?

  23. 23

    如何使用_.reduce(和_.each)从Underscore.js重写_.every / _。all

  24. 24

    如何使用_.reduce(和_.each)从Underscore.js重写_.every / _。all

  25. 25

    我如何在underscore.js的_.each循环的每次迭代中添加延迟?

  26. 26

    Underscore.js _.each函数的第三个参数如何工作?

  27. 27

    Expression matching each parameter inside delimited block

  28. 28

    Jpa persist/merge throws ConcurrentModificationException inside loop

  29. 29

    Don't loop code inside foreach bootstrap

热门标签

归档