How to use standard-as-callback - 10 common examples

To help you get started, we’ve selected a few standard-as-callback examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github luin / ioredis / lib / cluster / index.ts View on Github external
const status = this.status;
    this.setStatus("disconnecting");

    this.manuallyClosing = true;

    if (this.reconnectTimeout) {
      clearTimeout(this.reconnectTimeout);
      this.reconnectTimeout = null;
    }
    this.clearNodesRefreshInterval();

    this.subscriber.stop();

    const Promise = PromiseContainer.get();
    if (status === "wait") {
      const ret = asCallback(Promise.resolve("OK"), callback);

      // use setImmediate to make sure "close" event
      // being emitted after quit() is returned
      setImmediate(
        function() {
          this.setStatus("close");
          this.handleCloseEvent();
        }.bind(this)
      );

      return ret;
    }
    return asCallback(
      Promise.all(
        this.nodes().map(node =>
          node.quit().catch(err => {
github luin / ioredis / lib / cluster / index.ts View on Github external
const Promise = PromiseContainer.get();
    if (status === "wait") {
      const ret = asCallback(Promise.resolve("OK"), callback);

      // use setImmediate to make sure "close" event
      // being emitted after quit() is returned
      setImmediate(
        function() {
          this.setStatus("close");
          this.handleCloseEvent();
        }.bind(this)
      );

      return ret;
    }
    return asCallback(
      Promise.all(
        this.nodes().map(node =>
          node.quit().catch(err => {
            // Ignore the error caused by disconnecting since
            // we're disconnecting...
            if (err.message === CONNECTION_CLOSED_ERROR_MSG) {
              return "OK";
            }
            throw err;
          })
        )
      ).then(() => "OK"),
      callback
    );
  }
github luin / ioredis / lib / pipeline.ts View on Github external
Pipeline.prototype.exec = function(callback: CallbackFunction) {
  if (this._transactions > 0) {
    this._transactions -= 1;
    return (this.options.dropBufferSupport ? exec : execBuffer).apply(
      this,
      arguments
    );
  }
  if (!this.nodeifiedPromise) {
    this.nodeifiedPromise = true;
    asCallback(this.promise, callback);
  }
  if (!this._queue.length) {
    this.resolve([]);
  }
  let pipelineSlot: number;
  if (this.isCluster) {
    // List of the first key for each command
    const sampleKeys: string[] = [];
    for (let i = 0; i < this._queue.length; i++) {
      var keys = this._queue[i].getKeys();
      if (keys.length) {
        sampleKeys.push(keys[0]);
      }
    }

    if (sampleKeys.length) {
github luin / ioredis / lib / transaction.ts View on Github external
pipeline.exec = function(callback: CallbackFunction) {
      if (this._transactions > 0) {
        exec.call(pipeline);
      }

      // Returns directly when the pipeline
      // has been called multiple times (retries).
      if (this.nodeifiedPromise) {
        return exec.call(pipeline);
      }
      const promise = exec.call(pipeline);
      return asCallback(
        promise.then(function(result) {
          const execResult = result[result.length - 1];
          if (typeof execResult === "undefined") {
            throw new Error(
              "Pipeline cannot be used to send any commands when the `exec()` has been called on it."
            );
          }
          if (execResult[0]) {
            execResult[0].previousErrors = [];
            for (let i = 0; i < result.length - 1; ++i) {
              if (result[i][0]) {
                execResult[0].previousErrors.push(result[i][0]);
              }
            }
            throw execResult[0];
          }
github luin / ioredis / lib / script.ts View on Github external
return asCallback(
        result.catch(err => {
          if (err.toString().indexOf("NOSCRIPT") === -1) {
            throw err;
          }
          return container.sendCommand(
            new Command("eval", [this.lua].concat(args), options)
          );
        }),
        callback
      );
    }

    // result is not a Promise--probably returned from a pipeline chain; however,
    // we still need the callback to fire when the script is evaluated
    asCallback(evalsha.promise, callback);

    return result;
  }
}
github luin / ioredis / lib / script.ts View on Github external
options: any,
    callback?: CallbackFunction
  ) {
    if (typeof this.numberOfKeys === "number") {
      args.unshift(this.numberOfKeys);
    }
    if (this.keyPrefix) {
      options.keyPrefix = this.keyPrefix;
    }

    const evalsha = new Command("evalsha", [this.sha].concat(args), options);
    evalsha.isCustomCommand = true;

    const result = container.sendCommand(evalsha);
    if (isPromise(result)) {
      return asCallback(
        result.catch(err => {
          if (err.toString().indexOf("NOSCRIPT") === -1) {
            throw err;
          }
          return container.sendCommand(
            new Command("eval", [this.lua].concat(args), options)
          );
        }),
        callback
      );
    }

    // result is not a Promise--probably returned from a pipeline chain; however,
    // we still need the callback to fire when the script is evaluated
    asCallback(evalsha.promise, callback);
github luin / ioredis / lib / command.ts View on Github external
this.args = transformer(this.args);
        }
        this.stringifyArguments();
      }

      this.resolve = this._convertValue(resolve);
      if (this.errorStack) {
        this.reject = err => {
          reject(optimizeErrorStack(err, this.errorStack, __dirname));
        };
      } else {
        this.reject = reject;
      }
    });

    this.promise = asCallback(promise, this.callback);
  }
github luin / ioredis / lib / commander.ts View on Github external
var lastArgIndex = length - 1;
    var callback = arguments[lastArgIndex];
    if (typeof callback !== "function") {
      callback = undefined;
    } else {
      length = lastArgIndex;
    }
    var args = new Array(length - firstArgIndex);
    for (var i = firstArgIndex; i < length; ++i) {
      args[i - firstArgIndex] = arguments[i];
    }

    var options;
    if (this.options.dropBufferSupport) {
      if (!_encoding) {
        return asCallback(
          PromiseContainer.get().reject(new Error(DROP_BUFFER_SUPPORT_ERROR)),
          callback
        );
      }
      options = { replyEncoding: null };
    } else {
      options = { replyEncoding: _encoding };
    }

    if (this.options.showFriendlyErrorStack) {
      options.errorStack = new Error().stack;
    }
    if (this.options.keyPrefix) {
      options.keyPrefix = this.options.keyPrefix;
    }
github luin / ioredis / lib / redis / index.ts View on Github external
var connectionReadyHandler = function() {
          _this.removeListener("close", connectionCloseHandler);
          resolve();
        };
        var connectionCloseHandler = function() {
          _this.removeListener("ready", connectionReadyHandler);
          reject(new Error(CONNECTION_CLOSED_ERROR_MSG));
        };
        _this.once("ready", connectionReadyHandler);
        _this.once("close", connectionCloseHandler);
      }
    );
  });

  return asCallback(promise, callback);
};
github luin / ioredis / lib / redis / index.ts View on Github external
Redis.prototype.monitor = function(callback) {
  var monitorInstance = this.duplicate({
    monitor: true,
    lazyConnect: false
  });

  var Promise = PromiseContainer.get();
  return asCallback(
    new Promise(function(resolve) {
      monitorInstance.once("monitoring", function() {
        resolve(monitorInstance);
      });
    }),
    callback
  );
};

standard-as-callback

A performant and standard (Bluebird) library that registers a node-style callback on a promise

MIT
Latest version published 4 years ago

Package Health Score

65 / 100
Full package analysis

Popular standard-as-callback functions