Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set of asyncs called by set of asyncs! #5

Closed
des-des opened this issue Dec 3, 2015 · 7 comments
Closed

set of asyncs called by set of asyncs! #5

des-des opened this issue Dec 3, 2015 · 7 comments

Comments

@des-des
Copy link
Member

des-des commented Dec 3, 2015

Problem

I am trying to apply a pattern something like this:

var setThreeNumbersAsync = function(nums, callback) {
  var numbersFetched = 0;
  out = [];
  var next = function(number, index) {
    out[index] = number;
    numbersFetched += 1;
    if (numbersFetched === 3) {
      callback(out);
    }
  };
  setTimeout(function() {
    next(nums[0], 0);
  }, 100);
  setTimeout(function() {
    next(nums[1], 1);
  }, 200);
  setTimeout(function() {
    next(nums[2], 2);
  }, 300);
};

setThreeNumbersAsync([0, 1, 2], function(result) {
  console.log('0, 1, 2 set at time 0: ', result);
});

setTimeout(function() {
  setThreeNumbersAsync([3, 4, 5], function(result) {
    console.log('3, 4, 5 set at time 0.1: ', result);
  });
}, 50);

As expected the out put is this:

"0, 1, 2 set at time 0: "
[3, 4, 2]
"3, 4, 5 set at time 0.05: "
[3, 4, 5]

What is a nice way to do this. ie setThreeNumbersAsync is used/modified in such a way that each call made to it that holds a separate instance of out.

possible solutions

  • use new (but that would upset crockford)
  • have an array of arrays for out; each outer array referenced by a counter incremented on each call to setThreeNumbersAsync (plus some closure trickery)

questions

  • Is the second solution the best way to do things without using new?
  • Does this problem make sense?
@rjmk
Copy link

rjmk commented Dec 3, 2015

I'm not sure I understand the question. The code as written could simply be modified on line 3 to read var out = [] and this would keep out different with each call. I don't think this is what you're looking for though ...

@des-des
Copy link
Member Author

des-des commented Dec 3, 2015

derp. Ok this is not my problem, just a typo in my example...

@des-des des-des closed this as completed Dec 3, 2015
@rjmk
Copy link

rjmk commented Dec 3, 2015

@des-des I thought that was probably the case. Do reopen with an example that clarifies your problem!

@des-des
Copy link
Member Author

des-des commented Dec 3, 2015

ok need to do some bug hunting

@des-des
Copy link
Member Author

des-des commented Dec 3, 2015

@rjmk ok my issue was exactly as you pointed out. https:/dwyl/adoro/blob/engine/lib/markdown-amp.js#L7

@nelsonic
Copy link

nelsonic commented Dec 3, 2015

👍

@rjmk
Copy link

rjmk commented Dec 3, 2015

Happy to be a rubber duck

des-des added a commit to dwyl/adoro that referenced this issue Dec 3, 2015
des-des added a commit to dwyl/ampl that referenced this issue Jan 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants