Nothing Special   »   [go: up one dir, main page]

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

will be good to add example with async/await #308

Open
Scrum opened this issue Mar 6, 2020 · 7 comments
Open

will be good to add example with async/await #308

Scrum opened this issue Mar 6, 2020 · 7 comments

Comments

@Scrum
Copy link
Member
Scrum commented Mar 6, 2020

will be good to add example with async/await

Originally posted by @Grawl in #115 (comment)

@Scrum
Copy link
Member Author
Scrum commented Mar 6, 2020

@Grawl

will be good to add example with async/await

is there an example in mind?

@Grawl
Copy link
Contributor
Grawl commented Mar 12, 2020

no, I don't have this code in my projects

@winston0410
Copy link

Looking for a async/await example. My code doesn't work:

module.exports = (rootDir) => async (tree) => {
  // To fix: async issue/
  tree.match({ tag: 'img' }, async (node) => {
    const imagePath = node.attrs.src
    const dataUrl = await generatePlaceholder({
      imagePath: imagePath,
      sharp: {
        resizeRatio: 0.001
      },
      rootDir: rootDir
    })

    const modifiedNode = {
      ...node,
      attrs: {
        ...node.attrs,
        src: dataUrl,
        'data-src': imagePath
      }
    }

    console.log(modifiedNode)

    return await modifiedNode
  })
}

It always return a <div></div>, which is not what I want

@winston0410
Copy link

Hi I am still unable to solve this. It will be great if someone can help.

@Scrum
Copy link
Member Author
Scrum commented Dec 10, 2020

Hi I am still unable to solve this. It will be great if someone can help.

Hi, I saw your question, but unfortunately it affects methods !!!! and !!! which are used in 90% of cases. those. now you won't be able to use such an example.

import request from 'request'
import parser from 'posthtml-parser'

export default (tree) => {
    retrun tree.match({ tag: 'user-info' }, async (node) => {
      const {name} = await request(`/api/user-info?${node.attrs.dataUserId}`;
      node.content.push(name)
      return node;
    })
}

I am still thinking about this solution

@winston0410
Copy link

@Scrum Thank you for your response. I ended up using Promise to solve the async issue, but that is definitely not the best in terms of readability in my opinion.

@Scrum
Copy link
Member Author
Scrum commented Dec 15, 2020

There will be time and there will be a solution

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

No branches or pull requests

3 participants