reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

Promise.then should return a new promise

Open OliverJAsh opened this issue 11 years ago • 15 comments

var a = b.then(function () {
  return c;
});

a should become a promise for the value c. I believe this is defined in the Promises/A+ spec.

May I suggest that you use a promise library to ensure compliance with the spec?

OliverJAsh avatar Jan 27 '14 22:01 OliverJAsh

introducing a promise library creates a large dependency. if you're looking for strict compliance, you can add the dependency yourself, and wrap it.

One suggestion is to use when.js, and wrap the return value as a whenable promise.

var when = require('when')
var ajax = require('reqwest')

when(ajax('/path'))
  .then(fn1)
  .then(fn2)
  .otherwise(fail)

ded avatar Jan 27 '14 23:01 ded

I don't mind this idea, although I think it should be mentioned in the docs that the promise implementation isn’t compliant, along with what you suggest here.

Personally, I think if you're going to implement promises then it should at least conform to the spec. Including a library that does it for you would only be temporary, as soon promises will arrive natively in ES6. I don't think size is anything to worry about here; the minified version of bluebird is only 11 KB.

reqwest is a very popular library and should offer a complete solution IMO. If the goal is optimal performance, I think that belongs as a separate project. Unfortunately there is no decent alternative to reqwest.

OliverJAsh avatar Jan 27 '14 23:01 OliverJAsh

This is a common problem for browser libraries; because of the hassle involved with dependencies, it’s easier just to re-invent the wheel. However, I think that laziness almost always makes it more difficult for the consumer.

OliverJAsh avatar Jan 27 '14 23:01 OliverJAsh

This polyfill for ES6-style Promises is ~2 KB gzipped.

OliverJAsh avatar Jan 27 '14 23:01 OliverJAsh

ok, i think i'd rather just fix the troubles it is causing now. it shouldn't be too difficult to implement a small promise interface into the system and simply use that.

ded avatar Jan 28 '14 19:01 ded

I think it would be sensible to use the code someone already wrote for that (bring in a dependency), but in any case, as long as the promises behave according to the spec I’m happy!

OliverJAsh avatar Jan 30 '14 10:01 OliverJAsh

This project https://github.com/rse/thenable might be interesting for this issue. It aims to create an embeddable implementation of thenable objects (in 2kb compressed js), so no dependencies from npm nor bower are required.

cybrown avatar Jun 17 '14 15:06 cybrown

i'm moving more and more toward real promises, especially since they've officially landed natively in browsers. i'll put this issue at the top of my todo list.

ded avatar Jun 17 '14 15:06 ded

@ded - suggestion: make promises an optional plugin that plays nice with any spec-complying promise implementation.

Something like

var BPromise = require('bluebird');
reqwest.promiseLib(BPromise);

request.(...).then();

Advantages:

  1. The developer can use his or her promise library of choice
  2. Reduces filesize

https://github.com/matthewwithanm/httpplease.js takes this approach.

aldendaniels avatar Oct 02 '14 16:10 aldendaniels

+1 to the idea of a promise adapter. I did something similar for Argonaut: https://github.com/OliverJAsh/argonaut/blob/oja-promise-adapter/demo/index.html#L22

On 2 October 2014 18:24, Alden Daniels [email protected] wrote:

@ded https://github.com/ded - suggestion: make promises an optional plugin that plays nice with any spec-complying promise implementation.

Something like

var BPromise = require('bluebird');reqwest.promiseLib(BPromise); request.(...).then();

Advantages:

  1. The developer can use his or her promise library of choice
  2. Reduces filesize

https://github.com/matthewwithanm/httpplease.js takes this approach.

— Reply to this email directly or view it on GitHub https://github.com/ded/reqwest/issues/127#issuecomment-57656227.

OliverJAsh avatar Oct 02 '14 16:10 OliverJAsh

Any updates on this? I’ve also run into problems because of a lack of compliance with the spec.

iclanzan avatar Dec 11 '14 19:12 iclanzan

I was just bit by this today. Is there any update to be had? It is very confusing behavior to have an promise-like interface not conform to the spec. At the very least, there should at least be documentation in the README indicating this edge case.

Even better, this library should attempt to use global.Promise if available.

thomsbg avatar Oct 19 '15 22:10 thomsbg

it is at this hour I'm considering removing promise support, which would ultimately slim down the library, and allow implementors to include their own Promise library. It should be no responsibility of an http library to include Promise support anyhow.

ded avatar Oct 20 '15 16:10 ded

how about a metohd that takes a promise factory ?

cybrown avatar Oct 21 '15 07:10 cybrown

That could work too, but it still seems unnecessary. If I were implementing via promise, I would do it like this:

var when = require('when')
var reqwest = require('reqwest')
var ajax = function() {
  var args = arguments
  return when.promise(function (resolve, reject) {
    args.success = resolve
    args.error = reject
    reqwest.apply(null, args)
  }
}

or with a native Promise

var reqwest = require('reqwest')
var ajax = function() {
  var args = arguments
  return new Promise(function (resolve, reject) {
    args.success = resolve
    args.error = reject
    reqwest.apply(null, args)
  }
}

Then use:

ajax({ url: '/timeline', type: 'json' }).then(success, fail)

ded avatar Oct 21 '15 16:10 ded