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

When Iterate is not clear enough .... #454

Open
Maatary opened this issue May 13, 2015 · 4 comments
Open

When Iterate is not clear enough .... #454

Maatary opened this issue May 13, 2015 · 4 comments

Comments

@Maatary
Copy link

Maatary commented May 13, 2015

Generates a potentially infinite stream of promises by repeatedly calling f until predicate becomes true.

Where:

f - function that, given a seed, returns the next value or a promise for it.
predicate - function that receives the current iteration value, and should return truthy when the unfold should stop
***handler - function that receives each value as it is produced by f. It may return a promise to delay the next iteration.
seed - initial value provided to the handler, and first f invocation. May be a promise.

The question is what is handler for exactly ?

In my case:

1 - i have a table of values

2 - for each value i need to check if a value X, is not related to the value of the tables.

2.2 - In that respect obtaining the related element of a value requires making an AJAX call.

2.3 - Then upon getting the result of related value, i check if the value X is not in that result.

2.4 - If it is i'm done. If not i repeat the task with the next value in the table.

I have the feeling that iterate is suited for that.

1 - However i don't understand how the handler can help me.

2 - I feel one could give as a seed, an object {table: tab, iter: 0}, then the next function would simply change the iter value and return the object.

3 - The issue is with predicate and handler.

3.1 - I need to make an ajax call, get my result and compare. Can the result of predicate be a promise ?

@Maatary
Copy link
Author

Maatary commented May 13, 2015

Just for the record, what is "require" for ?

@Maatary
Copy link
Author

Maatary commented May 13, 2015

It is not clear to me what is the result of iterate, in the sense that when iterate stops, what argument is passed to done(). What it deferred resolve to ?

@briancavalier
Copy link
Member

Hey @Maatary. I'll try to answer some of your questions.

In general, when.iterate is for iteratively computing intermediate steps along the way to a final result. Each step may have some side effect, which is performed by the handler.

Does that help?

Just for the record, what is "require" for ?

Are you asking about the require() function, for example here?

what argument is passed to done()

The promise returned by when.iterate represents the value computed by the final iteration, that is, the final call to f. If you observe that promise by calling then or done on it, it will provide the final value to the callback you supply. For example:

when.iterate(...).then(function(x) {
    // x is the value from the final iteration of when.iterate
});

If you use done with no arguments, then you will not be able to observe the final result. That might be fine for your use case if you just care about the side effects that handler performs during the iterations. In that case, adding done() with no arguments at the end will simply cause a loud failure plus a stack trace if the iteration fails, which can be helpful for debugging.

If you haven't already read then vs. done, or the debugging promises section, you may find them to be helpful.

Hope that helps!

@Maatary
Copy link
Author

Maatary commented May 16, 2015

Could you let me know how with iterate I could solve my use case. The
handler to not return any value but perform a side effect. How can I know
that I need to stop iterating based on what the handler compute.

On each element of the table I want to know if A is a narrower concept or a
broader concept. So for each element I need to make an Ajax call get the
narrower or broader and compare. If I find A in the list of broader or
narrower I stop and return the element itself (or true) else I return a
failure (or false).

So the result of is_broader or is_narrower are promises. That either true
or false / element or failure.

Depending on their result I want to continue up the chain or not. And get
either true/the element or false/failure.

So long as handler is for side effect I don't see how I can impact the
iteration.
On Sat, May 16, 2015 at 4:09 PM Brian Cavalier [email protected]
wrote:

Hey @Maatary https://github.com/Maatary. I'll try to answer some of
your questions.

In general, when.iterate is for iteratively computing intermediate steps
along the way to a final result. Each step may have some side effect, which
is performed by the handler.

Does that help?

Just for the record, what is "require" for ?

Are you asking about the require() function, for example here
https://github.com/cujojs/when/blob/master/when.js#L12?

what argument is passed to done()

The promise returned by when.iterate represents the value computed by the
final iteration, that is, the final call to f. If you observe that
promise by calling then or done on it, it will provide the final value to
the callback you supply. For example:

when.iterate(...).then(function(x) {
// x is the value from the final iteration of when.iterate
});

If you use done with no arguments, then you will not be able to observe
the final result. That might be fine for your use case if you just care
about the side effects that handler performs during the iterations. In
that case, adding done() with no arguments at the end will simply cause a
loud failure plus a stack trace if the iteration fails, which can be
helpful for debugging.

If you haven't already read then vs. done
https://github.com/cujojs/when/blob/master/docs/api.md#promisethen-vs-promisedone,
or the debugging promises section
https://github.com/cujojs/when/blob/master/docs/api.md#debugging-promises,
you may find them to be helpful.

Hope that helps!


Reply to this email directly or view it on GitHub
#454 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants