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

promisescript

v2.0.0

Published

Simple promise-based script loader for modern browsers

Downloads

1,299

Readme

Promise Script

A simple JavaScript, JSON, and CSS loader built using Promises.

Install

npm install --save promisescript

Usage

var promisescript = require('promisescript');

function loadRecaptcha() {
  return promisescript({
    url: '//www.google.com/recaptcha/api/js/recaptcha_ajax.js',
    type: 'script',
    exposed: 'Recaptcha',
  }).then(function() {
    return new Promise(function(resolve) {
      Recaptcha.create('0123456789abcdef', 'recaptcha_widget', {
        theme: 'clean',
        callback: resolve
      });
    });
  });
}

loadRecaptcha().then(function() {
  console.log(Recaptcha.get_challenge());
}).catch(function(e) {
  console.error('An error loading or executing Recaptcha has occured: ', e.message);
});

API

Source Object

A source object is fully specified when it contains three properties:

  • url, the URL that should be fetched
  • type, one of "script", "style", or "json" which defines the type of the URL being fetched.
  • exposed, a string that is checked against the global object to determine if the script successfully loaded. A deeply nested property can be checked by providing a dot-separated string. This property is not required, but required to check if a script loaded successfully in Internet Explorer, as this browser does not have an error event.

A Single Source Object

This module accepts a single source object, and returns a single Promise that resolve if successfully loaded, or rejected if it fails.

Multiple Source Objects

Additionally this module can accept multiple source objects as an array. An array of Promises is returned, in the same order as provided.

Strings

If you’re feeling lazy, and don’t care about Internet Explorer support, you can provide a string or an array of strings that represents script or style URLs. When utilizing this functionality, this module assumes URLs representing styles end in “.css”, and all others are assumed to be scripts.

Clearing Cache

To avoid fetching the same URL multiple times, the Promise is cached, which is fine behavior most use cases. However, this cache can be cleared by calling promisescript.clear.

Promise Implementation

To avoid coupling to a specific Promise implementation, this module uses the any-promise module to get a Promise. By default, any-promise will return the browser's native Promises if found or otherwise throws an error. If you need to support browsers that don't have native Promises, or if you prefer another implementation, please see the any-promise documentation for how to register.

Notes

  1. If you make multiple requests to the same URL, it will only be fetched once and the same Promise will be returned.

License

MIT