Javascript Meteor

Meteor Fibers meet Promises meet Callbacks — A practical guide4 min read

Meteor Fibers meet Promises meet Callbacks — A practical guide

We’ve all been there before. You have a native asynchronous JavaScript function you need to use in your Meteor code, or maybe a Meteor function you’d like to invoke in your Meteor-agnostic code. After considerable time, you end up banging your head on the wall after realizing it won’t be as trivial as it first sounded.

Meteor uses these guys called Fibers under the hood to enable asynchronous execution. Fibers come from the time before JavaScript supported native Promises or async-await keywords. Their goal was to enable developers to write asynchronous code that looks synchronous, without a callback and Promise hell. Given their origin, Fibers aren’t too fond of native ES6 Promises. For some use cases, the Meteor framework provides useful utility functions for conversion between Fiber and non-Fiber functions, such as Meteor.wrapAsync or Meteor.bindEnvironment. However, these operate with callback-style asynchronous functions and don’t play too well with ES6 style async-await paradigm.

At Apify, we’ve been fans of Meteor since 2015 when we built our web scraping and automation platform on top of it. Over the years, we’ve encountered exactly these challenges. In this post, we’d like to share how you can deal with these kinds of conversions easily and effectively. Let’s befriend Fibers and Promises once for all!

Example functions

In the following examples, we’ll consider the three functions that all correspond to the same underlying business logic.

fnPromise
ES6 style asynchronous function which returns a promise.

https://medium.com/media/5340541fa3914d82f5ef3ed5b79b0898/href

fnCallback
Asynchronous function that accepts a callback as an argument. Note we assume that callback itself accepts 2 arguments, error (or null if no error) and value.

https://medium.com/media/1484ef770438e8217ced493da71ef990/href

fnFiber
Meteor async function which can be run synchronously (from a developer perspective) in Meteor code thanks to Fiber.

https://medium.com/media/2e6c1dd5145debc1996cec6ca884faf3/href

There are six possible directions you can perform conversions between these three functions.

Possible pairwise conversions between Callback, Promise and Fiber style functions

Conversions

In this section, we show how to perform these six conversions.

Converting Promise to Callback function

In general, it is not recommended to convert an async Promise function to a Callback one in order to avoid callback hell. But if you really need to, you can do it as follows.

https://medium.com/media/06feb59bd42d19c87350b9d22c0c823b/href

Converting Callback to Promise function

For the opposite direction, one can use native util.promisify() in Node.

https://medium.com/media/b0cf89cd3f42b49807f56437e8197d66/href

Converting Callback to Fiber function

The conversion from Callback to Meteor Fiber function is also quite straightforward. Use Meteor.wrapAsync() as follows:

https://medium.com/media/4adf42974055441bd6e4436b39c5ca9d/href

Converting Fiber to Callback function

For the opposite direction, you can use Meteor.bindEnvironment() as follows:

https://medium.com/media/2fbae741638318db05d88fb2cb6b9d22/href

Converting Fiber to Promise function

Quite likely, you are using ES6 promises in your application and you’re most interested in the final two conversions between Promise and Meteor functions. In order to convert from Meteor to a Promise function, you can use Meteor.bindEnvironment() together with creating a new Promise.

https://medium.com/media/fdc07c98cc8c7c583723814611f05c7d/href

Converting Promise to Fiber

For the other direction, you can use this utility to do the trick.

https://medium.com/media/b6781a5f1886a03631de1e50e148ab68/href

Then, you can use the utility as follows.

https://medium.com/media/480dd8aa25e1dfba5d986184313dc53a/href

Conclusion

For those of you who like pictures like me, the conversions look schematically as follows.

How to perform conversions between Promise, Callback, and Fiber style functions

And finally, for the geeks out there, we close with an example on how to convert Fiber function to itself by first converting it to Callback function, then to Promise function, and finally back to Fiber function again.

Warning: don’t try this at home :P.

https://medium.com/media/4a17bfdb3bb8bca50df5b6fcfe8910e8/href

So at the end of the day, Promises and Fibers can indeed live side by side, happily ever after!

Meteor Fibers meet Promises meet Callbacks — A practical guide was originally published in Meteor Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.

React Admin Templates and Themes

Pin It on Pinterest

Generated by Feedzy