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.

Is there any method like `most.defer()` and `most.create()`?

See original GitHub issue

I did not find most.defer() and most.create() in the API docs.

I need to defer a function call until it is subscribed.

most.defer(() => {
  result = getResult();
  return most.of(result);
})

How do I do to achieve my goal?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
briancavaliercommented, Jan 24, 2017

if the function f passed to the continueWith() only be called when subscribed

Yep, that’s correct. Closing since I believe this answers the original question.

Cheers!

0reactions
xareeleecommented, Jan 17, 2017

Can you help me understand the use case for a function like defer?

// without `defer`
function foo(options) {
  // has side-effect or inner dependency; 
  // would be triggered immediately (before the returned stream is subscribed)
  const settings = setup_settings(options);
  return most.of(settings);
}

// with `defer`
// we could wrap the whole implementation in the `defer`
function foo(options) {
  // defer the execution until subscribed
  return defer(() => {
    // only be triggered when the this stream is subscribed.
    const settings = setup_settings(options);
    return most.of(settings);
  });
}

Using defer to keep the impure function to be called only when it is subscribed. It’s important if the timing matters.

import { empty, continueWith, just } from 'most'

// Let's make our own defer() function
const defer = f => continueWith(f, empty())

// Use it
defer(() => just(getResult()))

I did not try this, but it seems OK if the function f passed to the continueWith() only be called when subscribed. Thanks for your help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Defer Keyword in Golang - GeeksforGeeks
In Go language, defer statements delay the execution of the function or method or an anonymous method until the nearby functions returns.
Read more >
What Does Mono.defer() Do? - Baeldung
Here, we'll look at the use of the defer method to delay the execution of a Mono publisher.
Read more >
The defer keyword in Swift: try/finally done right
Swift's defer keyword lets us set up some work to be performed when the current scope exits. For example, you might want to...
Read more >
Scripts: async, defer - The Modern JavaScript Tutorial
Just like defer , the async attribute is ignored if the <script> tag has no src . Dynamic scripts. There's one more important...
Read more >
Defer, Panic, and Recover - The Go Programming Language
A defer statement pushes a function call onto a list. The list of saved calls is executed after the surrounding function returns. Defer...
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