S
S
SomeHuman22021-12-19 18:02:37
JavaScript
SomeHuman2, 2021-12-19 18:02:37

How to synchronize an asynchronous function?

Hello, I'm minifying css with css-nano, however it can only work in asynchronous mode.
An example of a minimize function using css-nano

const postcss = require("postcss");
const cssnano = require("cssnano");
const cssNanoPresetAdvanced = require("cssnano-preset-advanced");

let MINIFY_CSS = function (code) {
          postcss(cssnano({ preset: cssNanoPresetAdvanced }))
            .process(code)
            .then(function (result) {
              console.log(result.css);
            });
        },

you can use for example However, the problems start when I try to return a value, and not just output to the console. This is necessary for the code chains to work, for example
MINIFY_CSS("html{background:green;}")
EXTRACT_CSS(MINIFY_CSS(require("!!raw-loader!sass-loader!./MyScss.scss")))

My attempt is in the form
let MINIFY_CSS= function (code) {
          let minified;
          async () => {
            await postcss(cssnano({ preset: cssNanoPresetAdvanced }))
              .process(code)
              .then(function (result) {
                minified = result.css;
              });
          };
          console.log(minified);
          return minified;
        },

It prints undefined, which means that the value is returned before the async function completes and nothing works...
How can I improve my code or synchronize the async function?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question