How to use the promisifyAll function from bluebird
Find comprehensive JavaScript bluebird.promisifyAll code examples handpicked from public code repositorys.
bluebird.promisifyAll is a function that takes an object with callback-style methods and converts them to Promise-based methods, allowing for easier handling of asynchronous operations.
GitHub: firewalla/firewalla
23 24 25 26 27 28 29 30 31 32
const userID = f.getUserID(); const childProcess = require('child_process'); const execAsync = util.promisify(childProcess.exec); const Promise = require('bluebird'); const redis = require('../../util/redis_manager.js').getRedisClient(); const fs = Promise.promisifyAll(require("fs")); const validator = require('validator'); const Mode = require('../../net2/Mode.js'); const rclient = require('../../util/redis_manager.js').getRedisClient(); const PlatformLoader = require('../../platform/PlatformLoader.js')
How does bluebird.promisifyAll work?
bluebird.promisifyAll works by taking an object that has functions using the traditional error-first callback pattern, and creating new Promise-based versions of those functions.
These Promise-based functions return a Promise object that can be used to handle the result of the original function. If the original function would have called its callback with an error object as its first argument, the Promise will be rejected with that same error object. If the function would have called the callback with a non-error value as its second argument, the Promise will be resolved with that value.
This conversion allows for easier handling of asynchronous operations, as Promises are a more modern and flexible way to handle async code compared to callbacks.
GitHub: labulakalia/ibm_bak
178 179 180 181 182 183 184 185 186
##### 清单 8\. 引入 bluebird 使 Node\_Redis API 异步化 ``` var Q = require('bluebird'); var rediz = require('redis'); Q.promisifyAll(rediz.RedisClient.prototype); Q.promisifyAll(rediz.Multi.prototype); ```
Ai Example
1 2 3 4 5 6 7 8 9 10
const fs = require("fs"); const Promise = require("bluebird"); // Convert the 'fs' object to use Promise-based functions Promise.promisifyAll(fs); // Use the new Promise-based functions fs.readFileAsync("example.txt", "utf8") .then((data) => console.log(data)) .catch((err) => console.error(err));
In this example, we first import the fs module, which provides functions for working with the file system in Node.js. We also import the Promise module from Bluebird, which we'll use to convert the fs object to use Promise-based functions. We then call Promise.promisifyAll(fs), which creates new Promise-based versions of all the functions on the fs object. This includes a new version of fs.readFile called fs.readFileAsync. Finally, we use fs.readFileAsync to read the contents of a file named 'example.txt', and handle the result using a Promise chain. If the read is successful, we log the contents of the file to the console. If an error occurs, we log the error to the console.
13 14 15 16 17 18 19 20 21 22
return schema.getAsync(keys) .then((document) => document && document.toJSON()); } function listDocuments(schema, { hashKey }) { return Promise.promisifyAll(schema.query(hashKey)) .loadAll() .execAsync() .get('Items') .map((document) => document.toJSON());
2 3 4 5 6 7 8 9 10 11
//---------// // Imports // //---------// const bPromise = require('bluebird'), bFs = bPromise.promisifyAll(require('fs')), child_process = require('child_process'), common = require('../common'), fp = require('lodash/fp'), madonnaFunction = require('madonna-function'),
34 35 36 37 38 39 40 41 42 43
const dbapi = require('../../db/api') module.exports = function(options) { var log = logger.createLogger('auth-mock') var app = express() var server = Promise.promisifyAll(http.createServer(app)) lifecycle.observe(function() { log.info('Waiting for client connections to end') return server.closeAsync()
GitHub: TruenoDB/trueno
390 391 392 393 394 395 396 397 398
/* Creates HTTP String Request to be used */ var strRequest = self._createHTTPStatusRequestString(jobId); /* Promisification */ Promise.promisifyAll(spark_job_request); /* Create new Promise with the spark job request */ return new Promise( function(resolve, reject) {
GitHub: prolificinteractive/ballad
0 1 2 3 4 5 6 7 8 9
#!/usr/bin/env node var path = require('path'); var _ = require('lodash'); var Promise = require('bluebird'); var fs = Promise.promisifyAll(require('fs')); var execAsync = Promise.promisify(require('child_process').exec); var program = require('commander'); var request = require('request'); var pkg = require('../package.json');
+ 9 other calls in file
20 21 22 23 24 25 26 27 28
const isElevated = Bluebird.promisify(require('is-elevated')); const ipc = require('node-ipc'); const _ = require('lodash'); const os = require('os'); const path = require('path'); const sudoPrompt = Bluebird.promisifyAll(require('sudo-prompt')); const utils = require('./utils'); const EXIT_CODES = require('../shared/exit-codes'); const packageJSON = require('../../package.json');
+ 19 other calls in file
GitHub: Comcast/karma
10 11 12 13 14 15 16 17 18 19
var Set = require('core-js/library/fn/set') var from = require('core-js/library/fn/array/from') var Promise = require('bluebird') var mm = require('minimatch') var Glob = require('glob').Glob var fs = Promise.promisifyAll(require('graceful-fs')) var pathLib = require('path') var _ = require('lodash') var File = require('./file')
959 960 961 962 963 964 965 966 967 968
cb(new Error('bar')) }) } } Promise.promisifyAll(obj) obj.successAsync() .then(function (value) { t.strictEqual(value, 'foo')
+ 2 other calls in file
4 5 6 7 8 9 10 11 12 13 14
const Promise = require('bluebird'); const concatStream = require('concat-stream'); const Docker = require('dockerode'); const shellescape = require('shell-escape'); const tmp = require('tmp'); const fs = Promise.promisifyAll(require('fs')); const {getCodeLimit, getTimeLimit} = require('../controllers/utils.js'); const langInfos = require('../data/infos.json'); const docker = new Docker();
+ 4 other calls in file
2 3 4 5 6 7 8 9 10 11
const http = require('http'); const BBPromise = require('bluebird'); const express = require('express'); const compression = require('compression'); const bodyParser = require('body-parser'); const fs = BBPromise.promisifyAll(require('fs')); const sUtil = require('./lib/util'); const apiUtil = require('./lib/api-util'); const packageInfo = require('./package.json'); const yaml = require('js-yaml');
+ 2 other calls in file
bluebird.reject is the most popular function in bluebird (2988 examples)