Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xhr-promise

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xhr-promise

Wrap the XMLHttpRequest object with Promise/A+ compliant promises.

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Build Status NPM version

xhr-promise

This module wraps the XMLHttpRequest object with Promise/A+ compliant promises. The promise implementation is provided by the bluebird promise library.

Browser support

Because xhr-promise uses the XMLHttpRequest object this library will work with IE7+, Safari 5+ and evergreen browsers (Chrome and Firefox). You should read the bluebird docs for workarounds with promises and IE 7 and IE 8.

Installation

This package is available on npm as:

npm install xhr-promise

Example

The xhr-promise code in this example does the same thing as the following XMLHttpRequest code.

xhr-promise code:

var XMLHttpRequestPromise = require('xhr-promise');

var xhrPromise = new XMLHttpRequestPromise();

xhrPromise.send({
    method: 'POST',
    url: 'https://example.com/form',
    data: 'foo=bar'
  })
  .then(function (results) {
    if (results.status !== 200) {
      throw new Error('request failed');
    }
    // ...
  })
  .catch(function (e) {
    console.error('XHR error');
    // ...
  });

XMLHttpRequest code:

var xhr = new XMLHttpRequest();

xhr.onload = function () {
  if (xhr.status !== 200) {
    throw new Error('request failed');
  }
  // ...
}

xhr.onerror = function () {
  console.error('XHR error');
  // ...
}

xhr.open('POST', 'https://example.com/form', true);

xhr.send('foo=bar');

Access to the XMLHttpRequest object

You still have direct access to the XMLHttpRequest instance if you want to access or manipulate the object state yourself.

var XMLHttpRequestPromise = require('xhr-promise');

var xhrPromise = new XMLHttpRequestPromise();

xhrPromise.send({...})
  .then(function () {
    var xhr = xhrPromise.getXHR();
  });

Running the tests

$ npm install
$ grunt test

License

MIT

Keywords

FAQs

Package last updated on 02 Dec 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc