Feature Request: Revise index.export() to return an Array of Promises
See original GitHub issueCurrently
- The function
index.export( function(key, data) {..} ), returns a single value - the booleantrue. - The registered callback function is invoked four times, once each for keys
reg,cfg,mapandctx.
Requested Improvement
-Adjust index.export() to return an array of promises - i.e., one promise for each prospective callback invocation. [Alternately, return an iterable suitable for a Promise.All() construct.]
Benefits
- The proposed would facilitate tracking (e.g., blocking on) the resulting N callbacks.
- Going forward, Flexsearch could adjust the number of callback invocations with less (if any) client impact.
- Errors (rejections) could be handled within the Promise framework.
Example
Simulate an exception in the callback function.
myErrantCallback (k, v) {
// Convert k to kx for https://github.com/nextapps-de/flexsearch/issues/273
let kx = k;
if (k.lastIndexOf('.') > 0) {
kx = k.substring(k.lastIndexOf('.') + 1);
}
// Throw a random exception 15% of the time.
if (Math.floor(Math.random() * 100) < 15) {
throw new Error(`k: ${k}, kx: ${kx}, v: ${v}`);
} else {
console.log(`k: ${k}, kx: ${kx}, v: ${v}`);
}
}
The current export() facility, requires clients to anticipate and track the number of callback invocations AND deal with any exceptions that arise. The proposed facility simplifies these tasks.
Promise.All(index.improvedExport( function(key, data) {..} )
.then(...) // Take an action after all N Promises are fulfilled.
.catch(...) // Centrally manage exceptions.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:9
- Comments:12 (1 by maintainers)
Top Results From Across the Web
How can I execute array of promises in sequential order?
Start() method). .all doesn't execute anything it just returns a promise. If you have an array of promise returning functions:
Read more >Promise.all() - JavaScript - MDN Web Docs
The Promise.all() method takes an iterable of promises as input and returns a single Promise . This returned promise fulfills when all of ......
Read more >Understanding the Event Loop, Callbacks, Promises, and ...
Callback functions are an effective way to ensure delayed execution of a function until another one completes and returns with data. However, ...
Read more >Dynamic imports - The Modern JavaScript Tutorial
The import(module) expression loads the module and returns a promise that resolves into a module object that contains all its exports.
Read more >FlexSearch.js: Next-Generation full text search library ... - GitHub
Every method called on a Worker index is treated as async. You will get back a Promise or you can provide a callback...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

If I had this, I could do something like:
I don’t think that
export()needs to return an array of promises. If you need an array of promises you can just create one and add the created promises in the callbacks, as seen in the example provided by @rpsirois . But you probably never need this.What doesn’t make sense is that
export()is an asynchronous function by nature that invokes a callback and yet it doesn’t provide any way to know when it’s done invoking callbacks for all keys. So users need to jump through hoops and compare the keys provided to callbacks with some list of possible keys.What’s needed is that
export()should return a single promise when it’s done, i.e. finished calling all callbacks, and, if needed, waiting for callbacks that return promises. Then resolve.So you can simply write:
By the way, the Typescript definitions incorrectly (for the actual code) type
export()as returningPromise<void>, which was very confusing.