Uploaded image for project: 'MongoDB Shell'
  1. MongoDB Shell
  2. MONGOSH-1362

Add way to explicitly wait for completion of async tasks

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Shell API
    • None
    • 5

      mongosh exits when it encounters the end of its input and no more commands are running. However, this is an issue when Node.js builtin async APIs are used; for example,

      cursor = db.getCollection("accounts").aggregate([{$match:{}}])
      cursorAsStream = stream.Readable.from(cursor.map(entry => EJSON.stringify(entry) + '\n'));
      cursorAsStream.pipe(fs.createWriteStream('accounts'));
      

      will truncate the resulting file when mongosh exits after the .pipe() call.

      As a workaround, I suggested on slack to add while (!dest.writableFinished) sleep(10); in this scenario, but that’s obviously not something that we really want to recommend as the official solution to this issue.

      We should think of a better way of solving this. It would be relatively easy to add a waitFor(promise) shell API, however, this would essentially mean committing to support for top-level await in mongosh scripts.

            Assignee:
            Unassigned Unassigned
            Reporter:
            anna.henningsen@mongodb.com Anna Henningsen
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: