Promises es6 and superagent

superagent vs axios
superagent async/await
superagent unprocessable entity
superagent cookies
superagent download file
superagent return promise
superagent promise all
javascript promises tutorial

I'm attempting to use es6 promises with superagent. I'm attempting to call a function that has a superagent request wrapped inside.

Request.post(buildReq).then(res => {
 if (res.ok) {//process res}
});

Here is the function wrapping superagent

  static post(params) {
    superagent
      .post(params.url)
      .send(params.payload)
      .set('Accept', 'application/json')
      .end((error, res) => {
        return this.Promise.resolve(res);
      })
      .bind(this);
  }

I'm getting an error

enter code here Uncaught TypeError: Cannot read property 'then' of undefined

When I change the return of the function to

static post(params) {
    return Promise.resolve(superagent
      .post(params.url)
      .auth(params.auth.username, params.auth.password)
      .send(params.payload)
      .set('Accept', 'application/json')
      .end((error, res) => {
        return this.Promise.resolve(res);
      })
    );
  }

It looks like the data is returned in my browser's dev tools, but I cannot get to it within the .then function. How can I get the response from the promise.

It doesn't matter what you're returning from the end method callback, as it asynchronously executed when you've get response and result of callback execution is nowhere used. Look here and here in the source code. end method returns this, so in your second example you're resolving superagent not response. To get response your post method must looks like:

static post(params) {
    return new Promise((resolve, reject) => {
        superagent
            .post(params.url)
            .auth(params.auth.username, params.auth.password)
            .send(params.payload)
            .set('Accept', 'application/json')
            .end((error, res) => {
                error ? reject(error) : resolve(res);
            });
    });
}

opentable/superagent-es6-promise: Add promise support , Miss any of our Open RFC calls?Watch the recordings here! » superagent-es6-​promise. 1.0.0 • Public • Published 5 years ago. Readme · ExploreBETA · 0  Promises are one of the most exciting additions to JavaScript ES6. For supporting asynchronous programming, JavaScript uses callbacks, among other things. However, callbacks suffer from problems like…

Sometimes you'd like to avoid an indentation level caused by new Promise(...) then you can use directly Promise.reject and Promise.resolve.

static post(params) {
    return superagent
            .post(params.url)
            .auth(params.auth.username, params.auth.password)
            .send(params.payload)
            .set('Accept', 'application/json')
            .end((error, res) => {
                return error ? Promise.reject(error) : Promise.resolve(res);
            });
    });
}

SuperAgent, The browser-ready, minified version of superagent is only 6 KB <script src="​https://polyfill.io/v3/polyfill.min.js?features=Array.from,Promise,Symbol,Object We've started using ES6 and for compatibility with Internet Explorer  superagent-es6-promise. Add promise support to Superagent. it was initially forked from superagent-bluebird-promise. Install. npm install superagent-es6-promise. Usage. Simply require this package instead of superagent. Then you can call .then() instead of .end() to get a promise for your requests.

This is a more consise version, in case you need it for a lot of requests

import request from "superagent";

const withPromiseCallback = (resolve, reject) => (error, response) => {
  if (error) {
    reject({error});
  } else {
    resolve(response.body);
  }
};

export const fetchSuggestions = (search) => new Promise((resolve, reject) =>
 request.
    get("/api/auth/get-companies/0/50").
    type("form").
    set("Accept", "application/json").
    query({
      search,
    }).
    end(withPromiseCallback(resolve, reject))
);

export const fetchInitialInformation = () => new Promise((resolve, reject) =>
  request.
    get("/api/auth/check").
    set("Accept", "application/json").
    end(withPromiseCallback(resolve, reject))
);

An incremental tutorial on promises in javascript, How to use. Requires ES6 Promises. Polyfill or set superagentPromisePlugin.​Promise with es6-promise or equivalent. var request = require('superagent'); var  ES6 Promise support #726. Closed kornelski wants to merge 1 commit into visionmedia: master from Superagent is not a promises implementation, though.

With ES6, you can use async/await with Promise and Generator support:

const res = await request.get(url);

superagent-es6-promise, <script src="https://polyfill.io/v3/polyfill.min.js?features=Array.from,Promise We'​ve started using ES6 and for compatibility with Internet Explorer you may need  Possible duplicate of Promises es6 and superagent – k0pernikus Jul 20 '18 at 9:12 hmm not sure, since i want to get rid of the callback when calling the login function – Gobliins Jul 20 '18 at 9:18

As of v2.0.0, superagent provides an ES6-compatible .then(). So your code could become

static post(params) {
return superagent
        .post(params.url)
        .auth(params.auth.username, params.auth.password)
        .send(params.payload)
        .set('Accept', 'application/json')
        .then((res) => {
            return res;
        });
}

superagent, The Promise.all() method takes an iterable of promises as an input, and returns a single Promise as an output. This returned promise will resolve when all of the  The Fetch API was introduced in 2015 with ES6. The generic Request and Response interfaces provide consistency while Promises permit easier chaining and async/await without callbacks.

Package, Compare npm package download statistics over time: bluebird vs npm vs promise vs q vs es6-promise vs superagent. React + Flux, ES6, Babel ActionCreate using json-server and super agent, data not in response. Hello I am attempting to use json-server to mock up the api of an React Flux ES6 app I am building. But when I user the superagent node module to make the request from the action creator the data in the callback is undefined. Here's my code

Package, The promises of a chain are nested like Russian dolls, but get popped like the top of a stack. The first promise in the chain is most deeply nested and is the first to pop. (promise D, (promise C, (promise B, (promise A) ) ) ) When a nextValue is a promise, the effect is a dynamic replacement.

Promise.all(), Agenda TA (Duncan) explains super agent Immutable copy ES6 to solidify our JavaScript 5 abilities In order to use es6 effectively we should understand our JS capabilities well Bind call apply / lexical vs dynamic scoping ES6 setup cli How to make it part of our gulp build ES6 Curates set of its functionality including…