Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

pass a callback funcion into an async function node js

const express = require('express');
const app = express();
const dns = require('dns');


let CheckUrl = function (url, done) {
    return new Promise((resolve, reject) => {
        dns.lookup(url, function(err, address) {
            console.log("err " , err)
            if (err) {
                resolve(false)
            } else {
                resolve(true)
            }

        });
    });
} 


app.post("/api/shorturl/new", async function(req, res) {

  try {

    let result = await CheckUrl(req.body.url);
    console.log("result " , result)
    res.send(result)
  }
  catch (error) {
    console.log("in catch error " , error)
    res.send(error)
  }
});

app.listen(3000)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #pass #callback #funcion #async #function #node #js
ADD COMMENT
Topic
Name
1+2 =