How to have a method wait, before returning result

Meteor.methods({ 'saveMyFile': function (data, name, start, size, written) { var dat = new Buffer(data, 'binary') var mode = data.start == 0 ? 'w' : 'a'; var fd = fs.openSync('/Users/NeighborHood/apps/' + name, mode) fs.write(fd, dat, 0, data.length, start, function (err) { fs.closeSync(fd) }) if (written === size) { var url; cloudinary.uploader.upload('/Users/NeighborHood/apps/' + name, function (r) { url = r.url console.log(url) return url; }) } else { return (written / size * 100).toFixed(2) + '%' } } })

When I call this method each time It returns some result(% written), the last time it I want to wait for cloudinary and then return the url, but it already returns undefined, and later consoles the result.

Here is a great article with a few ideas about how to handle api/external calls like these: https://themeteorchef.com/snippets/synchronous-methods/