How to use the using function from bluebird
Find comprehensive JavaScript bluebird.using code examples handpicked from public code repositorys.
bluebird.using is a function in the Bluebird library that creates a new resource scope and manages the lifetime of the acquired resources within the scope.
914 915 916 917 918 919 920 921 922 923 924
test('Promise.using', function (t) { t.plan(6) twice(function () { var trans = ins.startTransaction() Promise.using(getResource(), function (resource) { t.strictEqual(resource, 'foo') t.strictEqual(ins.currTransaction().id, trans.id) })
+ 2 other calls in file
324 325 326 327 328 329 330 331 332 333
* @param {object} data * @param {function} [callback] * @return {Promise} */ replaceById(id, data, callback) { return Promise.using(this._acquireWriteLock(), () => this._replaceById(id, data)).asCallback(callback); } /** * Replaces matching documents.
+ 49 other calls in file
How does bluebird.using work?
bluebird.using is a function in the Bluebird library that creates a new resource scope and manages the lifetime of the acquired resources within the scope. The function takes two arguments: a resource acquisition function and a resource release function. The resource acquisition function returns a promise that acquires the required resource, and the resource release function releases the resource when it is no longer needed. When the bluebird.using function is called, it creates a new scope and calls the resource acquisition function within that scope. If the acquisition function resolves the promise with a resource, the function creates a new promise and chains it with the resolved promise. This new promise represents the lifetime of the acquired resource. When the new promise is resolved or rejected, the function calls the release function to release the acquired resource. The release function is passed the acquired resource as an argument. If any errors occur during the resource acquisition or release, the function rejects the new promise with the error. Overall, bluebird.using is a useful utility function for managing the lifetime of acquired resources within a scope. It ensures that acquired resources are properly released when they are no longer needed, and provides a way to handle errors that occur during the acquisition or release process.
33 34 35 36 37 38 39 40 41 42 43
connection.release(); }); } function querySql (query, params) { return Promise.using(getSqlConnection(), function (connection) { console.log("Got connection from pool"); if (typeof params !== 'undefined'){ return connection.queryAsync(query, params); } else {
+ 30 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9 10
const Promise = require("bluebird"); const fs = Promise.promisifyAll(require("fs")); Promise.using(fs.openAsync("example.txt", "r"), (fileDescriptor) => { return fs.readFileAsync(fileDescriptor, "utf8").then((data) => { console.log(data); }); }).catch((err) => { console.error(err); });
In this example, we use bluebird.using to manage the lifetime of a file descriptor and ensure that it is properly closed when no longer needed. The Promise.using function takes two arguments: a promise that acquires the file descriptor using fs.openAsync, and a callback function that reads the contents of the file using fs.readFileAsync. When the bluebird.using function is called, it creates a new scope and acquires the file descriptor using fs.openAsync. If the promise resolves successfully, the function calls the callback function with the acquired file descriptor as an argument. The callback function reads the contents of the file using fs.readFileAsync and logs the data to the console. When the promise returned by the callback function is resolved, the bluebird.using function calls the release function to close the file descriptor. If any errors occur during the acquisition or release of the file descriptor, the bluebird.using function catches the error and logs it to the console. Overall, bluebird.using is a useful utility function for managing the lifetime of acquired resources within a scope, such as file descriptors or database connections.
bluebird.reject is the most popular function in bluebird (2988 examples)