reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

*really* make promises chainable?

Open togakangaroo opened this issue 10 years ago • 5 comments

I see this issue and the associated PR but it doesn't seem to make promises chainable, just then methods. For example you would expect the following

let r = reqwest({url: '/api/ProspectiveCustomer', method: 'post'})
            .then(() => r.request.getResponseHeader('Location') )
            .then((url) => reqwest({url: url, type: 'json'}) )
            .then((d) => console.log("this should be json", d))

to work, but it doesn't! Seems like the second then chains the returned promise into the the next parameter. Instead it's supposed to return a new promise that resolves when everything is done.

That's the way every other promise library I've used has worked at least...

togakangaroo avatar May 11 '15 04:05 togakangaroo

since this isn't a legit promise library... it's in desperate need of one. i'm open to suggestions for light-weight promise libs

ded avatar May 11 '15 21:05 ded

If you're using a library, browser that supports it, or polyfill, this has been my workaround:

// overwrite reqwest
// or you could make it a separate fn
( _reqwest => {
    // wrap `reqwest` with a deferred
    reqwest = ( ...args ) => {
        let d = Promise.defer();
        _reqwest( ...args ).then( d.resolve, d.reject );
        return d.promise;
    };
} )( reqwest );

let r = reqwest( { url, method, type, data } )
    .then( res => res.getResponseHeader( 'Location' ) )
    .then( url => ( { url, type } ) )
    .then( reqwest );

r.then( data => console.log( data ) );

joshq00 avatar May 26 '15 21:05 joshq00

It's even simpler than that:

Promise.resolve(reqwest(...))

odzb avatar Aug 27 '15 19:08 odzb

this might help someone... you need to pass the "r" object to Promise.resolve

    getJSON(data_url) {
          let r = 
              reqwest({
                  url: data_url,
                  method: 'GET',
                  crossOrigin: true,
                  type: 'json',
                  contentType: 'application/json', 
                  headers: {
                    'Authorization': 'JWT ' + LoginStore.jwt
                  }
              })
           return r;
       }

      requestMultiData(urls, name){
        let requests = [];
        for (let url of urls){
           let promise = Promise.resolve(this.getJSON(url));
           requests.push(promise);
        }

        Promise.all(requests).then(function(values) { 
          console.log(name +" "+values); 
        }).catch(function(e) {
                 console.log("Error retrieving data");
         });
      }

raptoria avatar Feb 11 '16 18:02 raptoria

Wrap reqwest like this and you'll get actual promises:

function reqwestAsPromise(opts) {
    return new Promise((resolve, reject) => {
        reqwest({
            ...opts,
            success: resolve,
            error: response => {
                if (response.responseText) {
                    return reject(JSON.parse(response.responseText));
                }

                return reject(response);
            }
        })
    });
}

kmkr avatar Feb 19 '16 14:02 kmkr