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.

Make defer() wait until request > 0 before calling Func0

See original GitHub issue

Currently the defer operator calls its supplied Func0 immediately upon subscription as documented. Because this operator is usually used to delay work of some kind, this can prematurely trigger that work before the downstream consumer actually requests a non-zero amount.

A failing test, to illustrate:

AtomicBoolean b = new AtomicBoolean();
Observable<Boolean> bo = Observable.defer(() -> Observable.just(b.get()));

TestSubscriber s = new TestSubscriber(0);
bo.subscribe(s);
s.assertNoValues();

b.set(true);
s.requestMore(1);
s.assertValues(true);

In this test, the “work” (aka b.get()) ran before the downstream consumer actually wanted a value produced. If you think of b.get() as, say, an HTTP request or something based on time, etc. the problem becomes more clear.

I haven’t looked, but I would guess that defer’s behavior was implemented prior to the backpressure concept being introduced.

I’ll leave it up to the library maintainers to determine the risk of changing the behavior, but I want to again note one thing which started this conversation (from comments in #3780): there currently doesn’t exist an easy, sane, stable API for deferring work that is backpressure aware as described above. So if it’s determined to be too risky to alter defer, fast-tracking fromCallable to stable and things like SyncOnSubscribe would be useful. This is of great concern to library developers, not so much application developers, where only stable APIs can safely be used.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
akarnokdcommented, Mar 31, 2016

I don’t support changing defer and perhaps fromCallable should be fast tracked to stable version.

0reactions
JakeWhartoncommented, Mar 31, 2016

Sounds good 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make the calling function wait until the ... - Stack Overflow
I have a button which when clicked opens a modal(dashboard_name) in which user enters some value. Based on the value after he clicks...
Read more >
Scripts: async, defer - The Modern JavaScript Tutorial
DOMContentLoaded event handler waits for the deferred script. It only triggers when the script is downloaded and executed. Deferred scripts keep ...
Read more >
A complete guide to the Swift defer statement - LogRocket Blog
Learn about Swift's defer statement and syntax, and examine several real-world use cases that you can use in practice.
Read more >
$q - AngularJS: API
The Deferred API. A new instance of deferred is constructed by calling $q.defer() . The purpose of the deferred object is to expose...
Read more >
Underscore.js _.defer() Function - GeeksforGeeks
The _.defer() function is used to invoke/call a function until ... It has similar functioning to setTimeOut() function with a delay of 0....
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