ajax-best-promise

0.4.0 • Public • Published

ajax-best-promise

Ajax with best promise - Ajax with stream data

extending npm-version downloads build coverage dependencies

language: English also available in: Spanish

Instalation

This is a client-side module. Download here

Use

AjaxBestPromise.get({
    url:'http://example.com:3333/service/do.php',
    data:{ alfa: 1, betha: 2}
}).then(function(result){
    console.log(result);
}).catch(function(err){
    console.log(err);
});

Chunked data

The main adventage of ajax-best-promise is the ability for process partial data in three flavors: onChunk, onLine, onJson

AjaxBestPromise.get({
    url:'http://example.com:3333/service/do.php',
    data:{ alfa: 1, betha: 2}
}).onChunk(function(partialText){
    console.log(partialText);
}).then(function(){
    console.log('done!');
}).catch(function(err){
    console.log(err);
});

haders

You can send headers in the header property and recieve it with the onHeader function.

AjaxBestPromise.put({
    url:'http://example.com:3333/service/',
    data:{ alfa: 1, betha: 2},
    headers:{
        Authorization: 'Bearer ' + token
    }
}).onHeaders(function(headers){
    newId = headers.Location;
}).then(function(){
    console.log('data inserted', newId);
}).catch(function(err){
    console.log('error inserting data', err);
});

Error handler

Other adventage of ajax-best-promise is the ability for reconstruct the error object.

AjaxBestPromise.get({
    url:'http://inexistent.com.ux/',
    data:{ alfa: 1, betha: 2}
}).then(function(result){
    console.log(result);
}).catch(function(err){
    console.log(err); // 404 Cannot GET inexistent.com.ux
    console.log(err.status); // 404
});

Modern JS

try{
    var newId: string
    await AjaxBestPromise.put({
        url:'http://example.com:3333/service/',
        data:{ alfa: 1, betha: 2},
        headers:{
            Authorization: 'Bearer ' + token
        }
    }).onHeaders(function(headers){
        newId = headers.Location;
    });
    console.log('data inserted', newId);
}catch(err){
    console.log('error inserting data', err);
}

Tests with real devices

NPM version Device OS nav obs
0.1.3 Samsung Galaxy Note 4 Android 6.0.1 Chrome Mobile 44.0.2403
0.1.3 iPad mini Retina iOS 8.4.0 Mobile Safari 8.0.0
0.1.3 VMWare WinXP IE 8.0.0 polyfill:many - fail:line stream & json stream

Licence

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i ajax-best-promise

Weekly Downloads

11

Version

0.4.0

License

MIT

Unpacked Size

19.6 kB

Total Files

4

Last publish

Collaborators

  • tute