Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
function-done
Advanced tools
Readme
Handles completion and errors for standard functions, generators, async functions, callbacks, promises, observables, child processes and streams.
Will run call the function on nextTick
. This will cause all functions to be async.
You are given a callback and want to know when it's finished. I found it useful for a testing framework. Specifically, I needed to know when the test had finished.
This is a fork of asyncDone with sync and generator functionality added. Thanks for the hard work at gulpjs/async-done
funcDone(fn, callback)
Takes a function to execute (fn
) and a function to call on completion (callback
).
fn([done])
Optionally takes a callback to call when async tasks are complete. If done parameter is not defined and function doesn't return Stream, Child Process, Promise, Observable, and is not a generator function, the function is assumed to be synchronous
// Async example
funcDone(function(done){
// do async things
setTimeout(function() {
// Call done function on finish
done(null, 2);
}, 10);
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
funcDone(function(){
// return Stream. emit end of finish
var read = fs.createReadStream(exists);
return read.pipe(new EndStream());
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
funcDone(function* (){
function delay(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time);
});
}
// yield as many times as necessary
yield delay(10);
yield delay(100);
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
funcDone(function (){
function delay(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time);
});
}
// return a Promise from the function
return delay(10).then(function() {
return delay(100);
});
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
funcDone(async function (){
function delay(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time);
});
}
// return a Promise from the function
await delay(10);
await delay(100);
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
funcDone(function(){
// return child process from the function
return cp.exec('echo hello world');
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
funcDone(function(){
return 2
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
// Async example
funcDone(function(done){
// do async things
setTimeout(function() {
// Call done function on finish
done(new Error('An Error Occurred'));
}, 10);
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
funcDone(function(){
// return Stream. emit end of finish
var read = fs.createReadStream(notExists);
return read.pipe(new EndStream());
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
funcDone(function* (){
throw new Error('Generator error');
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
funcDone(function (){
return Promise.reject(new Error('Rejected Promise'));
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
funcDone(async function (){
throw new Error('Async Error');
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
funcDone(function(){
// return child process error from the function
return cp.exec('not-an-executable hello world');
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
funcDone(function(){
throw new Error('function error');
}, function(error, result){
// `error` will be null on successful execution of the first function.
// `result` will be the result from the first function.
});
Sync
function (function takes 0 params and doesn't return any of the below types)
Callback
called
Stream
or EventEmitter
returned
Generator
returned
Child Process
returned
Promise
returned (Async Function)
Observable
returned
callback(error, result)
If an error doesn't occur in the execution of the fn
function, the callback
method will receive the results as its second argument. Note: Observable and some streams don't received any results.
If an error occurred in the execution of the fn
function, The callback
method will receive an error as its first argument.
Errors can be caused by:
done
callbackerror
event emitted on a returned Stream
, EventEmitter
or Child Process
Promise
onError
handler being called on an Observable
MIT
FAQs
Handles completion and errors for sync, callbacks, promises, observables and streams.
The npm package function-done receives a total of 23 weekly downloads. As such, function-done popularity was classified as not popular.
We found that function-done 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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.