Menu
Choose a product
Viewing: v1.7.x (latest)
Find another version
Scroll for more
Open source
pingAsync( target, [optsOrCallback], [callback] )
Sends ICMP echo requests (pings) to the specified target asynchronously.
Signature
JavaScript
await pingAsync(target, optsOrCallback, callback)Parameters
| Parameter | Type | Description |
|---|---|---|
| target | string | Hostname or IP address to ping. |
| optsOrCallback | PingOptions | PingCallback | Optional ping options or callback function. |
| callback | PingCallback | Optional callback function for ping results. |
Returns
| Type | Description |
|---|---|
| Promise<boolean> | Promise that resolves to true if the number of successful pings is greater than or equal to the threshold, otherwise false. |
Example
JavaScript
import { pingAsync } from "k6/x/icmp"
export default async function () {
const host = "8.8.8.8"
console.log(`Pinging ${host} with callback:`);
const opts = {
timeout: 1000,
retries: 2,
count: 5
};
const result = await pingAsync(host, opts, (err, { target, sent_at, received_at, seq, ttl, size, options }) => {
if (err) {
console.error(`${target}: ${err}`);
return
}
const rtt = received_at - sent_at;
console.log(`${size} bytes from ${target}: icmp_seq=${seq} ttl=${ttl} time=${rtt} ms`);
});
if (result) {
console.log(`Host ${host} is reachable`);
} else {
console.error(`Host ${host} is unreachable`);
}
}Was this page helpful?
Related resources from Grafana Labs
Additional helpful documentation, links, and articles:
Video

Performance testing and observability in Grafana Cloud
Optimize user experiences with Grafana Cloud. Learn real-time insights, performance testing with k6, and continuous validation with Synthetic Monitoring.
Events

User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.