Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
What function is passed to cb here?

So I have a small project containing both frontend (html) and backend (express: server, routers) parts. The project isn't that clean, so the its main operationality is launched directly in html section. And not much is clear to me here, especially what function is passed to cb (callback) here?

I have the following code in part of my html page within js project:

            const $ = document.getElementById.bind(document)

            const request = (path, cb) =>
                fetch(path)
                    .then((res) => {
                        if (res.ok) return res.json()
                        throw Error('HTTP error: ' + res.status)
                    })
                    .then(cb)
                    .catch((err) => ($('result').innerHTML = err))

            const main = async () => {
                const pinRequired = new URLSearchParams(document.location.search).get('pin')
                const id = await request(`./qrcode?pin=${pinRequired}`, (json) => {
                    const { qrbase64, deeplink, pin, id } = json
                    $('qrcode').innerHTML = `<img src="${qrbase64}" alt="Red dot" />`
                    $('deeplink').innerHTML = `<a href=${deeplink} target="_blank"> ${deeplink.slice(0, 90)}...</a>`
                    $('pin').innerHTML = pin ? pin : 'Not requested'
                    return id
                })

                setInterval(() => request(`./status?id=${id}`, ({ status }) => ($('result').innerHTML = status)), 1000)
            }

            main().catch(console.log)

Is this (json)? I also don't know why it is in () round brackets, however, it is an object, it cannot be passed as a callback, right?

And I also have a code in another file, which contains /qrcode route of my website. There is a function (quite big, so i'm not posting it, just pointing that it doesn't return function that may be passed as a callback).

If this callback 100% is in another part of the code, as you think, please let me know.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If what you're asking about is this callback (json) => { ... } in the code below:

               request(`./qrcode?pin=${pinRequired}`, (json) => {
                    const { qrbase64, deeplink, pin, id } = json
                    $('qrcode').innerHTML = `<img src="${qrbase64}" alt="Red dot" />`
                    $('deeplink').innerHTML = `<a href=${deeplink} target="_blank"> ${deeplink.slice(0, 90)}...</a>`
                    $('pin').innerHTML = pin ? pin : 'Not requested'
                    return id
                });

Then this is what is known as an arrow function. You can read about them here on MDN. They are a shortcut syntax for declaring a function that also has a number of implementation differences.

Note, there are some other issues in your code as request() does not return a promise so it does no good to use await on it and you won't get the id back from return id either.

Also note that the request library has been deprecated and generally shouldn't be used for new code. There is a list of alternatives here, all of which support promises natively. My favorite in that list is the got() library.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!