Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is svgson an async function? #46

Open
davedx opened this issue Sep 2, 2021 · 2 comments
Open

Why is svgson an async function? #46

davedx opened this issue Sep 2, 2021 · 2 comments

Comments

@davedx
Copy link

davedx commented Sep 2, 2021

Looking at the code, it seems that everything is synchronous, so I'm wondering why the export returns a promise?

@SignDawn
Copy link

SignDawn commented May 1, 2022

Yes, I tested it. The whole process is synchronous. Maybe it's better to use parseSync .
index.js

const { parse, stringify } = require('svgson')
console.log('before')
parse(`
  <svg>
    <line
      stroke= "#bada55"
      stroke-width= "2"
      stroke-linecap= "round"
      x1= "70"
      y1= "80"
      x2= "250"
      y2= "150">
    </line>
  </svg>`).then((json) => {
  console.log(JSON.stringify(json, null, 2))
})
console.log('after');

svgson.cjs.js

function svgson() {
  // console.log('parse', 'cjs')
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
    args[_key] = arguments[_key];
  }

  return new Promise(function (resolve, reject) {
    try {
      var res = svgsonSync.apply(void 0, args);
      console.log('resolve 前');
      resolve(res);
      console.log('resolve 后');
    } catch (e) {
      reject(e);
    }
  });
}

result:

before
resolve 
resolve 
after
{
  "name": "svg",
  "type": "element",
  "value": "",
  "attributes": {},
  "children": [
    {
      "name": "line",
      "type": "element",
      "value": "",
      "attributes": {
        "stroke": "#bada55",
        "stroke-width": "2",
        "stroke-linecap": "round",
        "x1": "70",
        "y1": "80",
        "x2": "250",
        "y2": "150"
      },
      "children": []
    }
  ]
}

@elrumordelaluz
Copy link
Owner

While have no impact on the main package, I am leaving both versions. However I want to hear some more examples, use cases allowing us to choose if remove the async layer, leaving everything sync and publishing it as a new major version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants