Automattic / juice

Juice inlines CSS stylesheets into your HTML source.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Promise version of `juiceResources`

niftylettuce opened this issue · comments

Can we add a Promise based version built-in? Maybe juiceResourcesAsync?

const juice = require('juice');

// promise version of `juice.juiceResources`
const juiceResources = (html, options) => {
  return new Promise((resolve, reject) => {
    juice.juiceResources(html, options, (err, html) => {
      if (err) return reject(err);
      resolve(html);
    });
  });
};

module.exports = juiceResources;

Good suggestion. You might also consider:

const pify = require('pify')
const inlineHtml = await pify(juice.juiceResources)(html)

Are folks using this now? https://nodejs.org/api/util.html#util_util_promisify_original

I've always leaned against supporting dual interfacing for node callback pattern and promises given how many libraries support back and forth on those. Its possibly also easier to just document how one would use one of these promisy libs than document there are two interfaces available.