Security News
NIST Misses 2024 Deadline to Clear NVD Backlog
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
The 'when' npm package is a robust library for working with asynchronous programming in JavaScript, particularly using promises. It provides utilities for creating, managing, and composing promises, making it easier to handle asynchronous operations and their potential complexities.
Creating Promises
This feature allows the creation of new promises. The code sample demonstrates how to create a simple promise that resolves with 'Hello, World!' after 1 second.
const when = require('when');
const promise = when.promise(function(resolve, reject) {
setTimeout(() => resolve('Hello, World!'), 1000);
});
promise.then(response => console.log(response));
Chaining Promises
This feature demonstrates chaining multiple promises. It shows how to perform a series of tasks sequentially, where each task starts only after the previous one has completed.
const when = require('when');
const cleanRoom = () => when.promise(resolve => resolve('Room cleaned'));
const removeTrash = () => when.promise(resolve => resolve('Trash removed'));
const winIcecream = () => when.promise(resolve => resolve('Won ice cream'));
cleanRoom()
.then(result => {
console.log(result);
return removeTrash();
})
.then(result => {
console.log(result);
return winIcecream();
})
.then(result => console.log(result));
Handling Errors
This feature involves error handling in promises. The code sample shows how to catch and handle errors that occur during the execution of promises.
const when = require('when');
const failTask = () => when.promise((resolve, reject) => reject('Failed task'));
failTask()
.then(result => console.log('Success:', result))
.catch(error => console.log('Error:', error));
Bluebird is a full-featured promise library with a focus on innovative features and performance. It is similar to 'when' but often cited for its superior performance and additional features like cancellation, progress tracking, and more detailed stack traces.
Q is one of the earliest promise libraries that influenced many others. It offers a similar API to 'when' but is generally considered to be less performant in modern applications. It provides a straightforward approach to handling asynchronous operations with promises.
The 'promise' package provides a minimalist implementation similar to the ES6 Promise specification. It is more lightweight compared to 'when' but lacks some of the more advanced features and utilities provided by 'when'.
When.js is cujoJS's lightweight Promises/A+ and when()
implementation that powers the async core of wire.js, cujoJS's IOC Container. It features:
It passes the Promises/A+ Test Suite, is very fast and compact, and has no external dependencies.
ensure
now ignores non-functions, like then
does, for consistency. (#207)MutationObserver
scheduler further reduces "time-to-first-handler" in modern browsers. (#198)
setImmediate
, MessageChannel
, and postMessage
unusable as fast task schedulers. Many thanks to @plaa and @calvinmetcalf for their help in discovering the problem and working out a solution. (#197)when.isPromiseLike
as the more accurately-named synonym for when.isPromise
.when.isPromise
. It can only tell you that something is "promise-like" (aka "thenable") anyway. Use the new, more accurately-named when.isPromiseLike
instead.when.all
and when.map
.promise.tap
for adding side effects to a promise chain.MessageChannel
scheduler reduces "time-to-first" handler, in environments that support it.when.defer().reject()
bypassing the unhandled rejection monitor. (#166)when/function
, when/callbacks
, and when/node/function
not preserving thisArg
. (#162)promise.yield
. (#164)when/monitor/console
.when.promise(resolver)
promise creation API. A lighter alternative to the heavier when.defer()
bindCallback
and liftCallback
in when/node/function
for more integration options with node-style callbacks.promise.yield
to workaround .NET minifier tools that don't yet understand ES5 identifier-as-property rules. See #157when.settle
that settles an array of promiseswhen/guard
generalized concurrency guarding and limitingpromise.inspect
for synchronously getting a snapshot of a promise's state at a particular instant.onFulfilled
, onRejected
, onProgress
handler arguments to when.all
, when.any
, when.some
. Use the returned promise's then()
(or otherwise()
, ensure()
, etc) to register handlers instead.
when.all(array).then(onFulfilled, onRejected)
instead of this: when.all(array, onFulfilled, onRejected)
. The functionality is equivalent.process
. Thanks Narsulwhen/keys
module with all()
and map()
for object keys/values.promise.ensure
as a better, and safer, replacement for promise.always
. See discussion as to why promise.always
is mistake-prone.
promise.always
lift()
is now the preferred name for what was bind()
in when/function, when/node/function, and when/callbacks.
bind()
in when/function
, when/node/function
, and when/callbacks
. Use lift()
instead.Get it
bower install when
or yeoman install when
, orgit clone https://github.com/cujojs/when
or git submodule add https://github.com/cujojs/when
Configure your loader with a package:
packages: [
{ name: 'when', location: 'path/to/when/', main: 'when' },
// ... other packages ...
]
define(['when', ...], function(when, ...) { ... });
or require(['when', ...], function(when, ...) { ... });
npm install when
var when = require('when');
ringo-admin install cujojs/when
var when = require('when');
git clone https://github.com/cujojs/when
or git submodule add https://github.com/cujojs/when
Add a transient define
shim, and a <script>
element for when.js
<script>
window.define = function(factory) {
try{ delete window.define; } catch(e){ window.define = void 0; } // IE
window.when = factory();
};
window.define.amd = {};
</script>
<script src="path/to/when/when.js"></script>
when
will be available as window.when
Note that when.js includes the Promises/A+ Test Suite. Running unit tests in Node will run both when.js's own test suite, and the Promises/A+ Test Suite.
npm install
npm test
npm install
npm start
- starts buster server & prints a urllocalhost:1111/capture
npm run-script test-browser
Much of this code was inspired by the async innards of wire.js, and has been influenced by the great work in Q, Dojo's Deferred, and uber.js.
2.5.1
ensure
now ignores non-functions, like then
does, for consistency. (#207)FAQs
A lightweight Promises/A+ and when() implementation, plus other async goodies.
The npm package when receives a total of 791,094 weekly downloads. As such, when popularity was classified as popular.
We found that when demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.