
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
async-done
Advanced tools
The async-done npm package is a utility for handling asynchronous tasks in Node.js. It provides a simple way to manage callbacks, promises, observables, and streams, making it easier to work with various types of asynchronous operations.
Handling Callbacks
This feature allows you to handle asynchronous tasks that use callbacks. The `asyncDone` function takes a task function and a callback to handle the result or error.
const asyncDone = require('async-done');
function callbackTask(done) {
setTimeout(() => {
done(null, 'Callback Task Completed');
}, 1000);
}
asyncDone(callbackTask, (err, result) => {
if (err) throw err;
console.log(result); // Output: Callback Task Completed
});
Handling Promises
This feature allows you to handle asynchronous tasks that return promises. The `asyncDone` function can manage the promise and handle the result or error.
const asyncDone = require('async-done');
function promiseTask() {
return new Promise((resolve) => {
setTimeout(() => {
resolve('Promise Task Completed');
}, 1000);
});
}
asyncDone(promiseTask, (err, result) => {
if (err) throw err;
console.log(result); // Output: Promise Task Completed
});
Handling Observables
This feature allows you to handle asynchronous tasks that return observables. The `asyncDone` function can manage the observable and handle the result or error.
const asyncDone = require('async-done');
const { Observable } = require('rxjs');
function observableTask() {
return new Observable((observer) => {
setTimeout(() => {
observer.next('Observable Task Completed');
observer.complete();
}, 1000);
});
}
asyncDone(observableTask, (err, result) => {
if (err) throw err;
console.log(result); // Output: Observable Task Completed
});
Handling Streams
This feature allows you to handle asynchronous tasks that return streams. The `asyncDone` function can manage the stream and handle the result or error.
const asyncDone = require('async-done');
const { Readable } = require('stream');
function streamTask() {
const stream = new Readable();
stream._read = () => {};
setTimeout(() => {
stream.push('Stream Task Completed');
stream.push(null);
}, 1000);
return stream;
}
asyncDone(streamTask, (err, result) => {
if (err) throw err;
console.log(result); // Output: Stream Task Completed
});
The async package provides a collection of functions for working with asynchronous JavaScript. It offers utilities for handling asynchronous control flow using callbacks, promises, and async/await. Compared to async-done, async provides a broader range of utilities for managing complex asynchronous workflows.
Bluebird is a fully-featured promise library for JavaScript. It provides advanced promise management features, including cancellation, iteration, and resource management. Compared to async-done, Bluebird focuses specifically on promises and offers more advanced promise-related functionalities.
RxJS is a library for reactive programming using observables. It provides powerful operators for composing asynchronous and event-based programs. Compared to async-done, RxJS focuses on observables and offers a comprehensive set of tools for working with reactive streams.
Manage callback, promise, and stream completion
Will run call the executor function on nextTick
. This will cause all functions to be async.
var asyncDone = require('async-done');
asyncDone(function(done){
// do async things
done(null, 2);
}, function(error, result){
// `error` will be undefined on successful execution of the first function.
// `result` will be the result from the first function.
});
var asyncDone = require('async-done');
asyncDone(function(done){
// do async things
done(new Error('Some Error Occurred'));
}, function(error, result){
// `error` will be an error from the first function.
// `result` will be undefined on failed execution of the first function.
});
asyncDone(executor, onComplete)
: FunctionTakes a function to execute (executor
) and a function to call on completion (onComplete
).
executor([done])
: FunctionOptionally takes a callback to call when async tasks are complete.
If a Stream
(or any instance of EventEmitter
) or Promise
is returned from the executor
function, they will be used to wire up the async resolution.
Streams
(or any instance of EventEmitter
) will be wrapped in a domain for error management. The end
and close
events will be used to resolve successfully.
Promises
will be listened for on the then
method. They will use the onFulfilled
to resolve successfully or the onRejected
methods to resolve with an error.
If you want to write a sync function, it will be executed on process.nextTick
and the results will be passed into onComplete
.
onComplete(error, result)
: FunctionIf an error doesn't occur in the execution of the executor
function, the onComplete
method will receive the results as its second argument.
If an error occurred in the execution of the executor
function, The onComplete
method will receive an error as its first argument.
Errors can be caused by:
done
callbackerror
event emitted on a returned Stream
or EventEmitter
Promise
MIT
FAQs
Allows libraries to handle various caller provided asynchronous functions uniformly. Maps promises, observables, child processes and streams, and callbacks to callback style.
The npm package async-done receives a total of 1,873,930 weekly downloads. As such, async-done popularity was classified as popular.
We found that async-done demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.