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.

feature: most.create for nodejs style callbacks

See original GitHub issue

Node has the below style of async API for their ecosystem. Seeing the most.create is going to be moved into its own project, can we get a simple/clean way to provide Node async functions to it to create a stream source? e.g. most.fromNodeCallback(myAsync)

function myAsync(val, cb) {
   const err = null;
   const result = "success";
   // do something async
   cb(err, result); // may be called once or multiple times
}

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
briancavaliercommented, Aug 8, 2016

@jadbox Now that @partially-applied has created mostify, and @most/create is available as an a la carte package, I feel like there are good solutions out there for now.

So, I’ll close this, but please feel free to continue discussion here.

1reaction
briancavaliercommented, May 21, 2016

Would native Node support be important enough to put into core?

My first instinct is that it should be a separate package, e.g. @most/node or something fairly obvious like that, but I’m open to discussing it. Browser apps likely wouldn’t need it 90% of the time, so being able to add it to a project a la carte is appealing.

Thoughts?

I’m curious why the Src/Sink method would be faster than most.create?

All most streams make the same guarantee: the function you pass to observe/forEach/reduce will not be called before observe/forEach/reduce returns. There are really two options for making that guarantee in most.create, either 1) wait for the stack to clear before calling the producer function passed to most.create, or 2) buffer events and propagate them after the stack clears.

Originally, it did 1, which proved to be confusing for users, so we switched to 2.

Since node functions already are async (well, they should be anyway!), by their very nature they would not propagate an event before the stack clears. So, there is no need to a lot of the buffering gymnastics that most.create does … it’d just be adding overhead for no reason.

There are a few other more minor factors. For example, most.create creates 3 closures to pass to the producer function. A node solution probably would only need 1. Also, because most.create is a general purpose solution, it has to do more generalized error handling. That means it uses try/catch in several places, which prevents the VM from optimizing several functions.

On the other hand, using Source/Sink directly is more “bare metal”. A node solution with Source/Sink would not need to do buffering, could create fewer closures, and could do error handling in a way that is more tailored to node style functions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What are callbacks? - Node.js
js uses callbacks. A callback is a function called at the completion of a given task; this prevents any blocking, and allows other...
Read more >
The callback pattern | Node.js Design Patterns
Callbacks are the materialization of the handlers of the reactor pattern and they are literally one of those imprints that give Node.js its...
Read more >
nodeJs callbacks simple example - Stack Overflow
First we create a callback function that accepts two arguments err and data . One problem with asynchronous functions is that it becomes ......
Read more >
Callback conventions in node.js, how and why - gists · GitHub
The following post explains the conventions that node.js uses for its callback patterns (referred to as Continuation-passing style) and how you should implement ......
Read more >
JavaScript Callback Functions – What are Callbacks in JS and ...
Callbacks make sure that a function is not going to run before a task is completed but will run right after the task...
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