Skip to content

Latest commit

 

History

History
60 lines (45 loc) · 1.42 KB

File metadata and controls

60 lines (45 loc) · 1.42 KB

node-roikoren/prefer-promises/dns

enforce require("dns").promises

Since Node.js v11.14.0, require("dns").promises API has been stable. Promise API and async/await syntax will make code more readable than callback API.

📖 Rule Details

This rule disallows callback API in favor of promise API.

Examples of 👎 incorrect code for this rule:

/*eslint node-roikoren/prefer-promises/dns: [error]*/
const dns = require("dns")

function lookup(hostname) {
  dns.lookup(hostname, (error, address, family) => {
    //...
  })
}
/*eslint node-roikoren/prefer-promises/dns: [error]*/
import dns from "dns"

function lookup(hostname) {
  dns.lookup(hostname, (error, address, family) => {
    //...
  })
}

Examples of 👍 correct code for this rule:

/*eslint node-roikoren/prefer-promises/dns: [error]*/
const { promises: dns } = require("dns")

async function lookup(hostname) {
  const { address, family } = await dns.lookup(hostname)
  //...
}
/*eslint node-roikoren/prefer-promises/dns: [error]*/
import { promises as dns } from "dns"

async function lookup(hostname) {
  const { address, family } = await dns.lookup(hostname)
  //...
}

🔎 Implementation