A small helper for getting only the last result of an asynchronous operation you perform multiple times in a row.
Pretty commonly, when performing asynchronous operations, you might fire off multiple of them, but then only care about the last result that comes back. A common example is searching:
But consider this scenario:
This isn't what you wanted! Once you sent off the search for "dog," you want to ignore any results from the previous search for "do."
Well, this package will solve your problems.
This package's main module has as its default export a single function,
last, which you can use to wrap naïve
functions into smart ones that will ignore all but the last result. It works with any function that returns a
Promises/A+-compliant promise. Once you do that, your code can look just as pretty as the
above example, but this time it will actually be correct!
var last = require"last";var smartSearch = lastdoSearch;$searchElon"input"smartSearchsearchElvaluethenupdateUIWithResultsdone;;
The wrapped function will return a Q promise. (In future versions, we'll try to return promises of the same type you pass in.) Once you call the function again, you will be guaranteed that previously-returned pending promises stay pending forever, and so you don't have to worry about them coming back later than your new promise.