Now it returns the value directly (or throws an error). Perfect for server-side methods!
const readFileSync = Meteor.wrapAsync(fs.readFile); const content = readFileSync('/path/to/file', 'utf8'); But remember: in Meteor 3, just use fs.promises.readFile with await . Progress! âš¡
If you're working with asynchronous code in Meteor (especially on the server), you've likely encountered Meteor.wrapAsync . meteor wrapasync
If you're still using Meteor.wrapAsync , you're writing legacy code (but it works!). Here's the modern take:
const result = await new Promise((resolve) => setTimeout(() => resolve('Done'), 1000) ); ✅ wrapAsync is great for converting Node.js style callbacks (error, result). ✅ But for modern Meteor 3+ — just use native async/await everywhere. Now it returns the value directly (or throws an error)
// Without wrapAsync (callback style) function fetchData(callback) { setTimeout(() => callback(null, { user: 'alice' }), 100); } // With wrapAsync const syncFetch = Meteor.wrapAsync(fetchData); const result = syncFetch(); // Blocks until done console.log(result.user); // 'alice'
import { Meteor } from 'meteor/meteor'; Meteor.methods({ 'getData'(id) { const syncGetData = Meteor.wrapAsync(legacyLibrary.getData); return syncGetData(id); } }); Progress
It converts a callback-based asynchronous function into a synchronous-looking one (using Fibers under the hood in Meteor 2.x and below).