node.jsnode-promisify

How does promisify know which variable to return?


I used util.promisify on this sqlite3 function:

// Before
db.each("Select * from example;", (error, row) => console.table(row));
db.each = util.promisify(db.each);
// After
async function getRow()
     try {
        console.table(await db.each("Select * from example;")); // line 7
     } catch (e) {
        console.error(e.message);
     }
}

My question is, how does promisify know which variable (the "err" or the "row") to return in line 7?


Solution

  • The util.promisify() is used/able to convert a callback into promise only if the first parameter of the callback function is the error parameter.

    So, it will always return "row" in Line 7. If there is an error, it will go to the catch block.