corejavascript / corejavascript/typeahead.js

Error calculating rendered variable in async function. Succesive calls to this funcion not appending the suggestions:

Open
#249 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
968
Forks
231
PR merge metrics
No merged PRs in 30d

Description

Hello!,

I noticed an error in the **async** function when calculating the value for the **rendered** variable... This makes succesive calls to this funcion not appending the suggestions:

```
function async(suggestions) {
suggestions = suggestions || [];
if (!canceled && rendered < that.limit) {
that.cancel = $.noop;
var idx = Math.abs(rendered - that.limit);
rendered += idx;
that._append(query, suggestions.slice(0, idx));
that.async && that.trigger("asyncReceived", query, that.name);
}
}
```

This way, the result of **rendered - limit** it's always added to **rendered**, so **rendered** will always end up having the value of **limit**!... For example, let's say we have already rendered 10 suggestions in a previous call to **async**, and that the **limit** is set to 1000. Then idx will be calculated as 1000-10=990... and thus when adding this amount to **rendered** (whose value is 10) the result will be again 1000 (the limit). This would make the next call to **async** to be over the limit and the new suggestions will not be added.

In my Iocal file I have solved it this way:

```
function async(suggestions) {
suggestions = suggestions || [];
if (!canceled && rendered < that.limit) {
that.cancel = $.noop;
var idx = Math.min(Math.abs(rendered - that.limit), suggestions.length);
rendered += idx;
that._append(query, suggestions.slice(0, idx));
that.async && that.trigger("asyncReceived", query, that.name);
}
}
```
It is as simple as applying a Math.max between the result of **rendered - limit** and **suggestion.length**.

Hope this helps someone.

Thank you.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.