How to use promise - 10 common examples

To help you get started, we’ve selected a few promise 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 kellyselden / create-react-app-updater / test / fixtures / ejected / local / my-app / config / polyfills.js View on Github external
if (typeof Promise === 'undefined') {
  // Rejection tracking prevents a common issue where React gets into an
  // inconsistent state due to an error, but it gets swallowed by a Promise,
  // and the user has no idea what causes React's erratic future behavior.
  require('promise/lib/rejection-tracking').enable();
  window.Promise = require('promise/lib/es6-extensions.js');
}

// fetch() polyfill for making API calls.
require('whatwg-fetch');

// Object.assign() is commonly used with React.
// It will use the native implementation if it's present and isn't buggy.
Object.assign = require('object-assign');
github mozilla / qbrt / test / run-path-missing-package.js View on Github external
* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. */

'use strict';

// Polyfill Promise.prototype.finally().
require('promise.prototype.finally').shim();

const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const spawn = require('child_process').spawn;
const tap = require('tap');

let exitCode = 0;
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'qbrt-test-'));

new Promise((resolve, reject) => {
  // Copy the app to a temporary directory to avoid qbrt finding
  // the package.json file for qbrt itself when looking for the package
  // for the test app.
  const sourceDir = path.join('test', 'hello-world-missing-package');
  const destDir = path.join(tempDir, 'hello-world-missing-package');
github infinispan / js-client / spec / utils / testing.js View on Github external
// Commons functions

var _ = require('underscore');

var log4js = require('log4js');

var promiseFinally = require('promise.prototype.finally');
promiseFinally.shim(); // will be a no-op if not needed

var readFile = require('fs').readFile;
var httpRequest = require('request');
var util = require('util');

var f = require('../../lib/functional');
var ispn = require('../../lib/infinispan');
var u = require('../../lib/utils');
var protocols = require('../../lib/protocols');

exports.local = {port: 11222, host: '127.0.0.1'};

exports.cluster1 = {port: 11322, host: '127.0.0.1'};
exports.cluster2 = {port: 11332, host: '127.0.0.1'};
exports.cluster3 = {port: 11342, host: '127.0.0.1'};
exports.cluster = [exports.cluster1, exports.cluster2, exports.cluster3];
github ember-cli / ember-cli / lib / tasks / server / middleware / tests-server / index.js View on Github external
if (!fs.existsSync(filePath) || !fs.statSync(filePath).isFile()) {
            // N.B., `baseURL` will end with a slash as it went through `cleanBaseURL`
            let newURL = `${baseURL}tests/index.html`;

            logger.info(
              'url: %s resolved to path: %s which is not a file. Assuming %s instead',
              req.path,
              filePath,
              newURL
            );
            req.url = newURL;
          }
        }
      });

      promiseFinally(promiseFinally(Promise.resolve(results), next), () => {
        if (config.finally) {
          config.finally();
        }
      });
    });
  }
github Azure / azure-sdk-for-node / test / services / servermanagement / servermanagement-tests.js View on Github external
suite.setupSuiteAsync(function (setUpDone) {

      // commonly used variables
      location = suite.Get('location', 'centralus');
      resourceGroupName = suite.Get('resourceGroupName', 'rsmt-rnr-rg');
      client = new ServerManagement(suite.credentials, suite.subscriptionId, baseUrl);

      // adapt the SMT client library to use promises.
      Promise.adaptToPromise(client.gateway);
      Promise.adaptToPromise(client.node);
      Promise.adaptToPromise(client.session);
      Promise.adaptToPromise(client.powerShell);

      setUpDone(); // We tell the test framework we are done setting up
      done(); // We tell mocha we are done with the 'before' part
    });
  });
github Azure / azure-sdk-for-node / test / services / servermanagement / servermanagement-tests.js View on Github external
suite.setupSuiteAsync(function (setUpDone) {

      // commonly used variables
      location = suite.Get('location', 'centralus');
      resourceGroupName = suite.Get('resourceGroupName', 'rsmt-rnr-rg');
      client = new ServerManagement(suite.credentials, suite.subscriptionId, baseUrl);

      // adapt the SMT client library to use promises.
      Promise.adaptToPromise(client.gateway);
      Promise.adaptToPromise(client.node);
      Promise.adaptToPromise(client.session);
      Promise.adaptToPromise(client.powerShell);

      setUpDone(); // We tell the test framework we are done setting up
      done(); // We tell mocha we are done with the 'before' part
    });
  });
github Azure / azure-sdk-for-node / test / services / servermanagement / servermanagement-tests.js View on Github external
suite.setupSuiteAsync(function (setUpDone) {

      // commonly used variables
      location = suite.Get('location', 'centralus');
      resourceGroupName = suite.Get('resourceGroupName', 'rsmt-rnr-rg');
      client = new ServerManagement(suite.credentials, suite.subscriptionId, baseUrl);

      // adapt the SMT client library to use promises.
      Promise.adaptToPromise(client.gateway);
      Promise.adaptToPromise(client.node);
      Promise.adaptToPromise(client.session);
      Promise.adaptToPromise(client.powerShell);

      setUpDone(); // We tell the test framework we are done setting up
      done(); // We tell mocha we are done with the 'before' part
    });
  });
github Azure / azure-sdk-for-node / test / services / servermanagement / servermanagement-tests.js View on Github external
Promise.adaptToPromise = function (fn, argumentCount) {
  if (typeof (fn) == 'object') {
    // adapt members of an object
    for (var each in fn) {
      if (typeof (fn[each]) == 'function' && !each.startsWith("begin")) {
        fn[each] = Promise.adaptToPromise(fn[each]);
      }
    }
    return;
  }
  if (typeof (fn) != 'function') {
    return;
  }
  // adapt a function.
  argumentCount = argumentCount || Infinity;
  return function () {
    var self = this;
    var args = Array.prototype.slice.call(arguments);
    return new Promise(function (resolve, reject) {
      while (args.length && args.length > argumentCount) {
        args.pop();
      }
github Azure / azure-sdk-for-node / test / services / servermanagement / servermanagement-tests.js View on Github external
suite.setupSuiteAsync(function (setUpDone) {

      // commonly used variables
      location = suite.Get('location', 'centralus');
      resourceGroupName = suite.Get('resourceGroupName', 'rsmt-rnr-rg');
      client = new ServerManagement(suite.credentials, suite.subscriptionId, baseUrl);

      // adapt the SMT client library to use promises.
      Promise.adaptToPromise(client.gateway);
      Promise.adaptToPromise(client.node);
      Promise.adaptToPromise(client.session);
      Promise.adaptToPromise(client.powerShell);

      setUpDone(); // We tell the test framework we are done setting up
      done(); // We tell mocha we are done with the 'before' part
    });
  });
github ngsankha / judgev2 / static.js View on Github external
(function() {
  var promise = require('promise'),
      fs = require('fs'),
      sh = require('child_process');

  var reportError = function(err) {
    console.log(err);
    throw err;
  };

  module.exports.reportError = reportError;

  // Promise representation of some Node.js function
  module.exports.mkdir = promise.denodeify(fs.mkdir);
  module.exports.writeFile = promise.denodeify(fs.writeFile);
  module.exports.readFile = promise.denodeify(fs.readFile);
  module.exports.exec = promise.denodeify(sh.exec);
})();