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

promise-value

v1.3.1

Published

A tiny wrapper for Promise to carry a promise and (if known) a value, plus a resolved flag -- this makes it easy to bridge between async promise-based code, and single-thread code.

Downloads

44

Readme

promise-value

A tiny wrapper for Promise to carry a promise and (if known) a value, plus a resolved flag.

This makes it easy to bridge between async promise-based code, and single-thread code.

Use-cases: e.g. within a React render() function.

Example code

import PromiseValue from 'promise-value';

// Make from a promise, e.g. an ajax call
let pvAjax = new PromiseValue($.get("https://bbc.co.uk"));

console.log(pvAjax.resolved, " = false");

// You can add a then() handler
pvAjax.promise.then(function(result) {
	console.log("Web-page get promise resolved", result);
});

// Error handling? just check pvAjax.error

// Or you can make a PromiseValue directly from a value
let pvInstant = new PromiseValue("hello");

console.log(pvInstant.resolved, " = true");

// once a promise resolves, you can get the value. Until then value is null.
console.log(pvInstant.value);

pvInstant.promise.then(function(result) {
	console.log("Instant Promise resolved to: "+result);
});

React Example: Using PromiseValue to manage web requests inside a render function

import React, {useState} from 'react';
import PromiseValue from 'promise-value';
import $ from 'jquery';

const MyAjaxWidget = () => {
	// What is the state?
	let [pvMyAjaxData, setpvMyAjaxData] = useState();
	// Hack: a dummy state var to trigger a react update
	let [dummy, setDummy] = useState();
	if ( ! pvMyAjaxData) {
		// Start the ajax call
		const pAjax = $.get("https://mysite.com/endpoint?foo=blah");		
		pvMyAjaxData = new PromiseValue(pAjax);
		setpvMyAjaxData(pvMyAjaxData); // Update the state, so we won't keep calling the server
		// trigger a react render when the response comes back (inc on error)
		pAjax.then(() => setDummy(":)"), () => setDummy(":("));
	}
	// Has the web request come back?
	if ( ! pvMyAjaxData.resolved) {
		// ...no -- return a spinner
		return <div className='spinner'>Loading...</div>;
	}
	// Error handling
	if (pvMyAjaxData.error) return <div>Web Request Failed :( {JSON.stringify(pvMyAjaxData.error)}</div>;
	// yes! We have data
	return <div>Lovely data! {JSON.stringify(pvMyAjaxData.value)}</div>;
};

Documentation

class PromiseValue {
	/** @type {!Promise} */
	promise;
	/** @type {!boolean} */
	resolved;
	/** @type {?Object} */
	value;
	/** @type {?Error} */
	error;
}

Call it like this: new PromiseValue(x) where x can be a Promise or a value.

The promise part of a PromiseValue is always set. The behaviour depends on the input x:

  • If it's a value -> you'll have a resolved Promise
  • If it's a Promise (or thenable) -> the input Promise
  • null/undefined -> rejected Promise

The value and error properties will be set instantly if known, and otherwise auto-set when the promise resolves. The resolved flag records the promise status, and changes to true once the promise is resolved.