Skip to content

Instantly share code, notes, and snippets.

@azu
Created April 5, 2014 16:18
Show Gist options
  • Save azu/9993939 to your computer and use it in GitHub Desktop.
Save azu/9993939 to your computer and use it in GitHub Desktop.
Web Notifications as promise
"use strict";
function notifyMessage(message) {
return new Promise(function (resolve, reject) {
if (Notification.permission === "granted") {
var notification = new Notification(message);
return resolve(notification);
} else if (Notification) {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
}
if (status === "granted") {
var notification = new Notification(message);
return resolve(notification);
} else {
reject(new Error("user denied"));
}
});
} else {
reject(new Error("doesn't support Notification API"));
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment