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.

Add .toPromise as analogy to .asCallback

See original GitHub issue

Very simple and useful addition:

Stream.prototype.toPromise = function() {
    return new Promise((resolve, reject) => {
       this.toCallback((err, res) => err ? reject(err) : resolve(res));
    });
};

Pros:

  • Enables direct integration with APIs designed to work with promises
  • Enables direct integration with language constructs like async/await

Cons:

  • Won’t work in Node 0.10 and Internet Explorer without Promise polyfill.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:20

github_iconTop GitHub Comments

3reactions
vqvucommented, Jun 6, 2017

I don’t think it’s so odd. Highland consumes a lot of things that it doesn’t let you get back out (like arrays or Iterables). In fact, streams are strictly more powerful than promises (since they can handle multiple asynchronous values instead of just one), so I would expect it to be easier to convert from Promises to streams than the other way around.

Plus, I’m not saying we shouldn’t have an easy way to convert back to a promise (toCallback was partly written to facilitate this). I’m just saying that since Promises aren’t completely universal yet, users need to take the (imo minor) step of providing a Promise constructor when they convert.

2reactions
vqvucommented, Jun 15, 2017

I understand that polyfills exist. They are not the problem. The problem isn’t even an implicit dependency on a global Promise—not by itself anyway. As I’ve said before, there’s nothing wrong with depending on Promise if it is core to the functionality of the library. Axios is a perfect example of such a library. Highland is not.

It comes down to a trade-off between developer user experience and implicit dependency on global state. For example, axios can choose to make all of its API require the user pass in a promise constructor, but that would be extremely annoying for their users. In their case, the improved user experience outweighs any issues that depending on Promise can cause.

For Highland, I think it’s the opposite. toPromise is only useful when you need to interop with libraries or language features that require promises. It’s use should be rare in comparison to other parts of the Highland API. The slight improvement in user experience from not requiring the promise constructor is not worth it. It’s a value judgement. You may disagree.

Note that #628 was merged, so I’m not completely opposed to the idea of a toPromise method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

toPromise - Learn RxJS
Convert observable to promise. ... ⚠ toPromise is not a pipable operator, as it does not return an observable.
Read more >
asCallback - Bluebird JS
asCallback will call the callback with an Error object if the promise's rejection reason is a falsy value. You can retrieve the original...
Read more >
rxjs observable.pipe(take(1)) vs toPromise - Stack Overflow
Actually the accepted answer is misleading by saying that transforming an observable to a promise is like taking a step backward.
Read more >
RxJS heads up: toPromise is being deprecated - InDepth.Dev
In RxJS 7 toPromise will become deprecated and with RxJS 8 it will be gone! So avoid using toPromise in your future development...
Read more >
HTTP - ts - TUTORIAL - Angular
import 'rxjs/add/operator/toPromise';. You'll add more operators, and learn why you must do so, later in this tutorial.
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