How to use the try function from bluebird

Find comprehensive JavaScript bluebird.try code examples handpicked from public code repositorys.

bluebird.try is a method that allows for error handling by attempting to execute a function and returning its result or error object.

44
45
46
47
48
49
50
51
52
53
  this.dbState = CONST.DB.STATE.TB_INIT;
  this.initialize();
}

initialize() {
  return Promise.try(() => {
    if (_.get(config, 'mongodb.provision.plan_id') === undefined && _.get(config, 'mongodb.url') === undefined) {
      logger.warn('Mongodb not configured. Either DB URL or Mongo Plan Id must be configured for enabling MongoDB usage with ServiceFabrik.', _.get(config, 'mongodb.url'));
      this.dbState = CONST.DB.STATE.NOT_CONFIGURED;
      return;
fork icon48
star icon40
watch icon19

+ 83 other calls in file

975
976
977
978
979
980
981
982
983
984
985
}


function parseAndCheckLogin(ctx, defaultFuncs, retryCount) {
    if (retryCount == undefined) retryCount = 0;
    return function(data) {
        return bluebird.try(function() {
            log.verbose("parseAndCheckLogin", data.body);
            if (data.statusCode >= 500 && data.statusCode < 600) {
                if (retryCount >= 5) {
                    throw {
fork icon3
star icon7
watch icon2

+ 32 other calls in file

How does bluebird.try work?

bluebird.try is a method in the Bluebird Promise library that provides a way to wrap a function call in a try-catch block and return a rejected Promise if an error is thrown within the function, allowing for easier error handling in Promise chains.

155
156
157
158
159
160
161
162
163
164
165
 */
function loadRoutes(app) {


    // get the list of files in routes/
    return fs.readdirAsync(`${__dirname}/routes`).map((fname) => {
        return BBPromise.try(() => {
            // ... and then load each route
            // but only if it's a js file
            if (!/\.js$/.test(fname)) {
                return undefined;
fork icon0
star icon4
watch icon12

+ 2 other calls in file

353
354
355
356
357
358
359
360
361
362
363


// (async () => {
//     const ig = new IgApiClient();
//     ig.state.generateDevice(igUsername);
//     // ig.state.proxyUrl = process.env.IG_PROXY;
//     Bluebird.try(async () => {
//         const auth = await ig.account.login(igUsername, igPassword);
//         console.log(auth);
//     }).catch(IgCheckpointError, async () => {
//         console.log(ig.state.checkpoint); // Checkpoint info here
fork icon0
star icon4
watch icon1

+ 29 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const Promise = require("bluebird");

function getUser(id) {
  // simulate an asynchronous operation
  return Promise.delay(100).then(() => {
    if (id !== 1) {
      throw new Error("User not found");
    }

    return { id: 1, name: "John Doe" };
  });
}

// usage example
Promise.try(() => getUser(1))
  .then((user) => console.log(user))
  .catch((err) => console.error(err.message));

In this example, getUser function returns a Promise that resolves with a user object or rejects with an error if the user is not found. The Promise.try method is used to wrap the call to getUser in a try/catch block, so that any errors thrown by the function can be caught and handled by the catch method attached to the Promise chain.

713
714
715
716
717
718
719
720
721
722
 *   Optionally run the query in a transaction.
 * @returns {Promise<undefined>}
 *   A promise resolving to the updated Collection where this method was called.
 */
detach(ids, options) {
  return Promise.try(() => this.triggerThen('detaching', this, ids, options))
    .then(() => this._handler('delete', ids, options))
    .then((response) => this.triggerThen('detached', this, response, options))
    .return(this);
},
fork icon0
star icon1
watch icon1

+ 7 other calls in file

101
102
103
104
105
106
107
108
109
110

  throw new BuildError('configuration error');
}

var buildStream = api.streams.create('src');
return Promise.try(function () {
    return buildExports.setupStreams(api, app, config);
  })
  .then(function () {
    return buildExports.getStreamOrder(api, app, config);
fork icon0
star icon0
watch icon2

+ 10 other calls in file

205
206
207
208
209
210
211
212
213
214
215
}


function prettyView (packument, manifest, opts) {
  // More modern, pretty printing of default view
  const unicode = opts.unicode
  return BB.try(() => {
    if (!manifest) {
      log.error(
        'view',
        'No matching versions.\n' +
fork icon0
star icon0
watch icon1

+ 6 other calls in file

1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
function parseAndCheckLogin(ctx, defaultFuncs, retryCount) {
  if (retryCount == undefined) {
    retryCount = 0;
  }
  return function (data) {
    return bluebird.try(function () {
      log.verbose("parseAndCheckLogin", data.body);
      if (data.statusCode >= 500 && data.statusCode < 600) {
        if (retryCount >= 5) {
          throw {
fork icon0
star icon0
watch icon1

+ 4 other calls in file

90
91
92
93
94
95
96
97
98
99
      return Promise.all(createIdxPrmList);
    });
}

static _createIndexV11 (options) {
  return Promise.try(() => this.QueryInterface.showIndex(this.getTableName(options), options))
    .then((indexes) => {
      // Assign an auto-generated name to indexes which are not named by the user
      this.options.indexes = this.QueryInterface.nameIndexes(this.options.indexes, this.tableName);
      indexes = _.filter(this.options.indexes, item1 =>
fork icon0
star icon0
watch icon2

+ 5 other calls in file

15
16
17
18
19
20
21
22
23
var contributors;

this.preload = function(userIds) {
  if (userIds.isEmpty()) return;

  return Promise.try(function() {
    if (options.includeRolesForTroupe) {
      return options.includeRolesForTroupe;
    }
fork icon0
star icon0
watch icon0

+ 3 other calls in file