npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dom-futures

v0.0.1

Published

DOM promises for browsers and node.js

Downloads

4

Readme

DOM Futures

DOM Promises for browsers and node.js. Acts like a polyfill in browsers without support for DOM Promises, and NPM package for node.js applications.

DOM Promises is a quiet new spec introduced by WhatWG. Promises provide a convenient way to get access to the result of an operation. You can use this package either in browser (it acts like a polyfill if your browser is missing support for DOM Promises) or in your node.js code. This feature used to be called futures.

Installation

  • node.js: npm install dom-futures
  • browser: use this file

Usage

function fetchJSON(url) {
	return new Promise(function (resolver) {
		var xhr = new XMLHttpRequest();
		xhr.open("GET", url, true);
		xhr.responseType = "json";

		xhr.onload = function () {
			if (xhr.response)
				resolver.resolve(xhr.response);
			else
				resolver.reject(new DOMError("JSONError"));
		};

		xhr.onerror = xhr.onabort = function (evt) {
			resolver.reject(new DOMError(evt.type));
		};

		xhr.send();
    });
}

// okay, now check this:
fetchJSON("/user/posts").then(showPosts, showFailcat);

As you can see, you can create chains with promises by passing a fulfillCallback function which returns a new Promise. If your fulfillCallback returns some value, the promise will be resolved with it. Check this:

fetchJSON("/users").then(function (json) {
	var firstUserId = json.users[0].id;
	return fetchJSON("/users/" + firstUserId);
}).then(function (json) {
	var userCommentsUrl = json.comments.url;
	return fetchJSON("/comments/" + userCommentsUrl);
}).then(function () {
	// and so on...
});

DOM Promises also have a bunch of helpers, or so-called "static methods".

Promise.every()

Returns a promise that is fulfilled or rejected when all values are fulfilled or any is rejected. You should pass promises to this function.

// browser example
function loadResource(url) {
	return new Promise(function (resolver) {
		var xhr = new XMLHttpRequest;
		xhr.withCredentials = true;
		xhr.open("GET", url, true);

		xhr.onload = function (evt) {
			resolver.resolve(evt.target.responseText);
		};

		xhr.onerror = function (evt) {
			resolver.reject(evt.type);
		};

		xhr.send();
	});
}

Promise.every(loadResource("http://google.com"), loadResource("http://stackoverflow.com")).then(function (htmlContents) {
	// htmlContents is 2-elements array
}, function (err) {
	// handle err
});

// node.js example
var Promise = require("dom-futures").Promise;
var fs = require("fs");

function readFile(path) {
	return new Promise(function (resolver) {
		fs.readFile(path, "utf-8", function (err, contents) {
			if (err) {
				resolver.reject(err);
			} else {
				resolver.resolve(contents);
			}
		});
	});
}

Promise.every(readFile("/etc/hosts"), readFile("/etc/group"), readFile("/etc/shadow")).then(function (filesContents) {
	// filesContents is 3-elements array
}, function (err) {
	// handle err
});

Promise.some()

Returns a promise that is fulfilled or rejected when one of values is fulfilled or all are rejected. You should pass promises to this function.

Promise.any()

Returns a promise that is fulfilled or rejected when any of values is either fulfilled or rejected. You should pass promises to this function.

Promise.fulfill(value)

Returns a promise that is fulfilled with result value. This is the easiest way to create a new fulfilled Promise with "value" as a promise result and "fulfilled" as a promise state.

Promise.resolve(value)

Returns a promise that depends upon value. In fact this kind of static method allows you to create a promises chain from the beginning. If you pass a promise object, the state of returned promise will be set as "pending" and result will be undefined. But if you pass smth other than promise, this will act like you call Promise.fulfill(value).

Promise.reject(value)

Returns a promise that is rejected with result value.

Finally