question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Revise index.export() to return an Array of Promises

See original GitHub issue

Currently

  • The function index.export( function(key, data) {..} ), returns a single value - the boolean true.
  • The registered callback function is invoked four times, once each for keys reg, cfg, map and ctx.

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:closed
  • Created 2 years ago
  • Reactions:9
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
peterbecommented, Oct 18, 2021

If I had this, I could do something like:

const bits = await Promise.all(index.export)
await fs.writeFile(bits, JSON.stringify(bits))
2reactions
danielvycommented, Jun 2, 2022

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:

await index.export();

By the way, the Typescript definitions incorrectly (for the actual code) type export() as returning Promise<void>, which was very confusing.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found