How to wrap a function wtihin an NPM lib that returns a Promise with the official promise package?


#1

I’ve been trying to get a node.js filepicker package to work with Meteor. This package utilizes promises via the node-promise packages. AS I’ve been reading it seems that the standard Meteor.wrapAsync will not correctly wrap promised based functions because they do not expect a callback as the last input.

I’ve seen this question on how to wrap promises but I can’t figure out how to get it to work with the above packages functions.

Also there is now an official meteor promise package so I am wondering how one might solve this with that package.

Below is the code I have been playing with to no avail.

var blob, aPromise, metadata, filepicker, nodeFilepicker;
nodeFilepicker = Meteor.npmRequire("node-filepicker");

filepicker = new nodeFilepicker('API KEY HERE');

blob = {
    url: 'https://www.filepicker.io/api/file/9BWnyKPBQI23ukbT7sZA'
};

aPromise  =  filepicker.stat(blob).then(function(metadata) {
  metadata = JSON.parse(metadata);
  console.log('It\'s metadata snitches', metadata);
  return metadata
});

Promise.await(aPromise);
console.log("aPromise"+ aPromise); // = empty object
console.log("metadata"+ metadata); // = undefined

The code inside the filepicker.stat runs and the metadata within the stat function is correct, however the Promise.await does not seem to yield execution, and the two console.logs at the end run before the stat function does and have no useful information.

I’ve also posted this on stackoverflow for anyone that likes those sweet internet points.


Fibers and meteor-promise npm pacakge
#2

Assuming you’re on the server, you can use Futures:

const Future = Npm.require('fibers/future');
const f = new Future();
filepicker.stat(blob).then((metadata) => {
  const metadata = JSON.parse(metadata);
  console.log('It\'s metadata snitches', metadata);
  f.return(metadata)
})
.catch(f.throw);
const metadata = f.wait();

Not sure how you’d do it using the new promise package to wrap the Promise.