1.3.1 • Published 8 years ago

asyncexit v1.3.1

Weekly downloads
-
License
LGPL-3.0
Repository
-
Last release
8 years ago

Usage

NEW: We now hook into the process global object to conform with how other asyncExiters do their thing

How to use:

require('asyncexit')()

process.on('asyncExit', function() {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            console.log("Goodbye, synchronous exiting!")
            resolve()
        }, 500)
    })
})

process.asyncExit()

If listeners is null or does not have a length (i.e, not an array), then asyncExit immediately calls process.exit

If code is not provided it defaults to 0

If timeout is not provided it defaults to 1000 ms

If the function has been waiting to exit for greater than timeout ms, then process.exit will be called immediately with an informing log message

Listeners

process.on('asyncExit', function() {
    doSynchronousClose()
    return new Promise(function (resolve, reject) {
        doAsyncClose().then(function() {
            resolve()
        }).catch(function(err) {
            reject(err)
        })
    })
})

The function will call all listeners with no arguments. If function returns nothing, it is assumed to have resolved synchronously.

Any listener that returns an object, it expects it to be a Promise-like object (i.e, then-able). The listener can then resolve or reject that promise as they need to.

IMPORTANT: As per the default behaviour of Promise.all(), if any promise in the array rejects, the entire promise is immediately rejected. This means that rejecting a promise results in an immediate exit with a message printed to std error. If you want to continue the async exit, then you should handle error printing yourself and resolve the promise instead of rejecting it.

1.3.1

8 years ago

1.3.0

8 years ago

1.2.4

8 years ago

1.2.3

8 years ago

1.2.2

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago