Promise.then should return a new promise
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?
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)
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.
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.
This polyfill for ES6-style Promises is ~2 KB gzipped.
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.
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!
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.
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 - 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:
- The developer can use his or her promise library of choice
- Reduces filesize
https://github.com/matthewwithanm/httpplease.js takes this approach.
+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:
- The developer can use his or her promise library of choice
- 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.
Any updates on this? I’ve also run into problems because of a lack of compliance with the spec.
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.
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.
how about a metohd that takes a promise factory ?
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)