Giter VIP home page Giter VIP logo

superagent-as-promised's Introduction

Note: as I understand, this module is no longer needed. SuperAgent as of 1.0.0 should now offer the same semantics as this module, since 2.0.0 returns a real Promise, and since 2.3.0 offers a .catch method.

SuperAgent as Promise(d)

SuperAgent as Promise.

The returned Promise will reject both on SuperAgent error, and on error in the response.

Compare with the promise plugin, which has similar semantics but a different interface.

Also, SuperAgent 2.0 now returns native Promises from req.then (but it sticks to its original semantics).

Installation

npm install superagent-as-promised

Usage

var request = require('superagent');
require('superagent-as-promised')(request);

Then

request
.get('/location')
.then( function(response) {
  console.log("Got "+response.text);
})
.catch( function(error) {
  console.dir(error);
})

is syntactic sugar for:

var promise = request
  .get('/location')
  .endAsync();

promise
  .then( function(response) {
    console.log("Got "+response.text);
  })
  .catch( function(error) {
    console.dir(error);
  })

Options

require('superagent-as-promised')(SuperAgent,Promise);

SuperAgent must be a SuperAgent class; it is extended with endAsync(), then, and catch methods. The optional Promise parameter allows you to provide your own Promise class; the native Promise class is used by default.

superagent-as-promised's People

Contributors

shimaore avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

chipbell4

superagent-as-promised's Issues

Accept an optional Promise

Hello,

It would be useful if superagent-as-promised optionally accepted a Promise object during initialization, similar to superagent-promise.

My use case involves extending the promise prototype to include an additional method. superagent-promise doesn't work well for my use case, because I'm actually consuming superagent via supertest which doesn't provide a hook to replace the superagent library (and superagent-promise is simply a wrapper - it doesn't modify superagent's prototype).

I'm happy to create a patch, if you're open to the idea.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.