Skip to content

Commit 0f03988

Browse files
committed
feat: support ES6 Promise
1 parent 7386813 commit 0f03988

File tree

7 files changed

+100
-23
lines changed

7 files changed

+100
-23
lines changed

README.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This module is for the browser only as it depends on the brower's `Image` API.
2020
```javascript
2121
var p = new Ping();
2222

23+
// Using callback
2324
p.ping("https://github.com", function(err, data) {
2425
// Also display error if err is returned.
2526
if (err) {
@@ -28,6 +29,15 @@ p.ping("https://github.com", function(err, data) {
2829
}
2930
document.getElementById("ping-github").innerHTML = data;
3031
});
32+
33+
// You may use Promise if the browser supports ES6
34+
p.ping("https://github.com")
35+
.then(data => {
36+
console.log("Successful ping: " + data);
37+
})
38+
.catch(data => {
39+
console.error("Ping failed: " + data);
40+
})
3141
```
3242

3343
or import as a module:
@@ -58,21 +68,18 @@ var p = new Ping(opt);
5868

5969
Create Ping instance.
6070

61-
#### options
62-
Type: `Object`
63-
64-
`favicon` Override the default `favicon.ico` image location.
71+
#### `options`
6572

66-
`timeout` Optional - Set a timeout in milliseconds.
73+
* Type: `Object`
74+
* `favicon`: Override the default `favicon.ico` image location.
75+
* `timeout`: Optional - Set a timeout in milliseconds.
6776

6877
### p.ping(source, callback)
6978

70-
Creates a ping request to the `source`.
71-
72-
`source` IP address or website URL, including protocol and port (optional). Example: `https://example.com:8080`
73-
74-
`callback(err, ping)` Callback function which returns an error and the response time in milliseconds.
79+
Creates a ping request to the `source`. Returns a promise that resolves and rejects to the response time in milliseconds, or if the browser does not supports Promise, returns undefined.
7580

81+
* `source`: IP address or website URL, including protocol and port (optional). Example: `https://example.com:8080`
82+
* `callback(err, ping)`: Optional - Callback function which returns an error and the response time in milliseconds.
7683

7784
## Development
7885

demo/index.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<body>
1313
<script src="../src/ping.js" type="text/javascript"></script>
1414

15+
<h2>Callback</h2>
1516
<ul>
1617
<li>google.com <span id="ping-google"></span></li>
1718
<li>guildbit.com <span id="ping-guildbit"></span></li>
@@ -20,6 +21,13 @@
2021
<li>am.guildbit.com <span id="ping-guildbit-am"></span></li>
2122
</ul>
2223

24+
<h2>Promise</h2>
25+
<ul>
26+
<li>facebook.com <span id="ping-facebook"></span></li>
27+
<li>reddit.com <span id="ping-reddit"></span></li>
28+
<li>linkedin.com <span id="ping-linkedin"></span></li>
29+
</ul>
30+
2331
<script src="https://gist.github.com/alfg/c44d8b061972879c9121.js"></script>
2432

2533
<script>
@@ -70,6 +78,30 @@
7078
document.getElementById("ping-guildbit-am").innerHTML = data;
7179
});
7280

81+
p.ping("http://facebook.com")
82+
.then(function(data) {
83+
document.getElementById("ping-facebook").innerHTML = data;
84+
})
85+
.catch(function(data) {
86+
document.getElementById("ping-facebook").innerHTML = data + " error";
87+
})
88+
89+
p.ping("http://reddit.com")
90+
.then(function(data) {
91+
document.getElementById("ping-reddit").innerHTML = data;
92+
})
93+
.catch(function(data) {
94+
document.getElementById("ping-reddit").innerHTML = data + " error";
95+
})
96+
97+
p.ping("http://linkedin.com")
98+
.then(function(data) {
99+
document.getElementById("ping-linkedin").innerHTML = data;
100+
})
101+
.catch(function(data) {
102+
document.getElementById("ping-linkedin").innerHTML = data + " error";
103+
})
104+
73105
</script>
74106
</body>
75107
</html>

dist/ping.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* ping.js - v0.2.2
2+
* ping.js - v0.3.0
33
* Ping Utilities in Javascript
44
* http://github.com/alfg/ping.js
55
*
@@ -20,11 +20,19 @@ var Ping = function(opt) {
2020

2121
/**
2222
* Pings source and triggers a callback when completed.
23-
* @param source Source of the website or server, including protocol and port.
24-
* @param callback Callback function to trigger when completed. Returns error and ping value.
25-
* @param timeout Optional number of milliseconds to wait before aborting.
23+
* @param {string} source Source of the website or server, including protocol and port.
24+
* @param {Function} callback Callback function to trigger when completed. Returns error and ping value.
25+
* @returns {Promise|undefined} A promise that both resolves and rejects to the ping value. Or undefined if the browser does not support Promise.
2626
*/
2727
Ping.prototype.ping = function(source, callback) {
28+
var promise, resolve, reject;
29+
if (typeof Promise !== "undefined") {
30+
promise = new Promise(function(_resolve, _reject) {
31+
resolve = _resolve;
32+
reject = _reject;
33+
});
34+
}
35+
2836
var self = this;
2937
self.wasSuccess = false;
3038
self.img = new Image();
@@ -57,18 +65,29 @@ Ping.prototype.ping = function(source, callback) {
5765
if (timer) { clearTimeout(timer); }
5866
var pong = new Date() - start;
5967

60-
if (typeof callback === "function") {
68+
if (!callback) {
69+
if (promise) {
70+
return this.wasSuccess ? resolve(pong) : reject(pong);
71+
} else {
72+
throw new Error("Promise is not supported by your browser. Use callback instead.");
73+
}
74+
} else if (typeof callback === "function") {
6175
// When operating in timeout mode, the timeout callback doesn't pass [event] as e.
6276
// Notice [this] instead of [self], since .call() was used with context
6377
if (!this.wasSuccess) {
6478
if (self.logError) { console.error("error loading resource"); }
79+
if (promise) { reject(pong); }
6580
return callback("error", pong);
6681
}
82+
if (promise) { resolve(pong); }
6783
return callback(null, pong);
84+
} else {
85+
throw new Error("Callback is not a function.");
6886
}
6987
}
7088

7189
self.img.src = source + self.favicon + "?" + (+new Date()); // Trigger image load with cache buster
90+
return promise;
7291
};
7392

7493
if (typeof exports !== "undefined") {

dist/ping.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ping.js",
33
"description": "Ping Utilities with Javascript",
4-
"version": "0.2.3",
4+
"version": "0.3.0",
55
"author": "Alf <[email protected]>",
66
"main": "index.js",
77
"devDependencies": {

pingjs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"title": "ping.js",
44
"description": "Ping Utilities in Javascript",
55
"keywords": ["javascript", "ping", "utilities"],
6-
"version": "0.2.2",
6+
"version": "0.3.0",
77
"author": {
88
"name": "Alfred Gutierrez",
99
"email": "[email protected]",

src/ping.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ var Ping = function(opt) {
1212

1313
/**
1414
* Pings source and triggers a callback when completed.
15-
* @param source Source of the website or server, including protocol and port.
16-
* @param callback Callback function to trigger when completed. Returns error and ping value.
17-
* @param timeout Optional number of milliseconds to wait before aborting.
15+
* @param {string} source Source of the website or server, including protocol and port.
16+
* @param {Function} callback Callback function to trigger when completed. Returns error and ping value.
17+
* @returns {Promise|undefined} A promise that both resolves and rejects to the ping value. Or undefined if the browser does not support Promise.
1818
*/
1919
Ping.prototype.ping = function(source, callback) {
20+
var promise, resolve, reject;
21+
if (typeof Promise !== "undefined") {
22+
promise = new Promise(function(_resolve, _reject) {
23+
resolve = _resolve;
24+
reject = _reject;
25+
});
26+
}
27+
2028
var self = this;
2129
self.wasSuccess = false;
2230
self.img = new Image();
@@ -49,18 +57,29 @@ Ping.prototype.ping = function(source, callback) {
4957
if (timer) { clearTimeout(timer); }
5058
var pong = new Date() - start;
5159

52-
if (typeof callback === "function") {
60+
if (!callback) {
61+
if (promise) {
62+
return this.wasSuccess ? resolve(pong) : reject(pong);
63+
} else {
64+
throw new Error("Promise is not supported by your browser. Use callback instead.");
65+
}
66+
} else if (typeof callback === "function") {
5367
// When operating in timeout mode, the timeout callback doesn't pass [event] as e.
5468
// Notice [this] instead of [self], since .call() was used with context
5569
if (!this.wasSuccess) {
5670
if (self.logError) { console.error("error loading resource"); }
71+
if (promise) { reject(pong); }
5772
return callback("error", pong);
5873
}
74+
if (promise) { resolve(pong); }
5975
return callback(null, pong);
76+
} else {
77+
throw new Error("Callback is not a function.");
6078
}
6179
}
6280

6381
self.img.src = source + self.favicon + "?" + (+new Date()); // Trigger image load with cache buster
82+
return promise;
6483
};
6584

6685
if (typeof exports !== "undefined") {

0 commit comments

Comments
 (0)