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.

Using async/await in doto -> toPromise resolves to early

See original GitHub issue

My Problem is related to the version 3.0.0-beta.7 Tried to find some related issues but I didn’t find anything close to it.

I have been using an AWS Lambda and the Highland libary with some functionality on top of it. (With the new use operator).

Everything worked like a charm until I started to use doto with an async function. When I try to consume the stream via toPromise(...), it seems that the Promise resolves before every asynchronous doto function resolved/finished. The resulting problem is, that the overall promise resolves too early, my Lambda considers his tasks as finished and aborts every ongoing request (as the process is ending) that is still pending. However, that does not happen if I use the done operator with an callback (So no promises involved).

The question now is, as doto is only for side effects and async side effects are somehow hard to “track”, could this be considered a bug or just a coincidence that has to be considered?

Thanks in advance.

Code Snippets

return ...
    .publishToIoTBroker(`${event.topic}/valid`) // <- My custom operator
    .toPromise(Promise);
function publishToIoTBroker(...) {
...
return this.doto(async data => {
...
await iotBroker.publish(...).promise()
});
}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
eccentric-jcommented, Mar 27, 2019

I don’t anticipate that will work as intended, but I could be wrong.

Instead I would structure it like this:

const _ = require('highland');

// Used to generate random delays
function rand (max) {
  return Math.floor(Math.random() * Math.floor(max));
}

// Create a promise that resolves at a random delay of time
function promise (max) {
  return new Promise((resolve) => {
    setTimeout(() => resolve(Date.now()), rand(max));
  });
}


_([1, 2, 3])
  .map(async dataINeedToPropagate => {
    const promiseResult = await promise(1000);

    return { promiseResult, dataINeedToPropagate };
  })
  .flatMap(_)
  .each(({ promiseResult, dataINeedToPropagate }) => console.log(
      'received', promiseResult, dataINeedToPropagate
  ));

// =>
// received 1553699474556 1
// received 1553699475217 2
// received 1553699475405 3
1reaction
vqvucommented, Mar 27, 2019

This is working as intended. doto is only meant for synchronous side effects or asynchronous side effects for which you don’t care to wait.

If you’re interested in waiting for the side effect to complete, use flatMap instead. There is no async version of doto.

return this.map(async data => {
      ...
      return data;
    })
    // coerces the promises back to streams and wait on them.
    .flatMap(_)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Async/await
If await gets a non-promise object with .then , it calls that method providing the built-in functions resolve and reject as arguments (just...
Read more >
How to make async/await wait for an Observable to return
Next to the await, the function with .toPromise() says "its not a function", but only on testing environment. ¿How do you test it?...
Read more >
Understand promises before you start using async/await
So there's a way of writing it with promises, and a way of writing it with async/await. Why do I need to care...
Read more >
Use Promise.all to Stop Async/Await from Blocking ...
In async functions, await blocks any code that follows from executing until the Promise has resolves, which means that our refactored code doesn't...
Read more >
Simplifying Angular with Async/Await
First of all, as much as I love the async / await pattern, it's not a silver ... forEach(function(file) { file = path.resolve(dir,...
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