Skip to content

Instantly share code, notes, and snippets.

@vaidd4
Created January 12, 2021 22:50
Show Gist options
  • Select an option

  • Save vaidd4/22131e7bd061b85ca1c919163a6a688e to your computer and use it in GitHub Desktop.

Select an option

Save vaidd4/22131e7bd061b85ca1c919163a6a688e to your computer and use it in GitHub Desktop.

Revisions

  1. vaidd4 created this gist Jan 12, 2021.
    28 changes: 28 additions & 0 deletions cbtoasync.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    function cbtoasync(fncb, ...params) {
    return new Promise((res, rej) => {
    fncb(...params, (...data) => {res(...data)})
    })
    }

    function tr_cbtoasync(fncb) {
    return (...data) => cbtoasync(fncb, ...data)
    }

    /* Tests */

    function testcb(data, cb) {
    cb(data)
    }

    testcb("hello", d => console.log(d + " world"))

    cbtoasync(testcb, "hi").then(d => d + " dude").then(console.log)

    const test = tr_cbtoasync(testcb)

    test("yo").then(d => d + " bud").then(console.log)

    ;(async () => {
    const h = await test("hola")
    console.log(h + " amigo")
    })()