Git Product home page Git Product logo

jsonpy's Introduction

jsonpy

jsonpy is a tiny library for work with JSONP. Callback mechanism based on the CommonJS Promises/A pattern, so you can provide multiple callbacks for any possible state.

Usage

The simpliest example of usage is calling jsonpy like an ordinary function:

// Single callback
jsonpy('url', function(response) {
	// process data
});

// ...with error handling
jsonpy('url', function(response) {
	// process data
}, function() {
	// handle error
});

// ...add always callback
jsonpy('url', function(response) {
	// process data
}, function() {
	// handle error
}, function() {
	// this code will be executed always
});

But hey, what if we want to customize something? We may pass to jsonpy an object:

jsonpy({
	// JSONP url.
	// Required.
	url: 'url',

	// If the name of parameter containing callback name differs from 'callback',
	// you can customize it by setting 'field' option.
	// Optional.
	field: 'jsonCallback',

	// You can set a timeout for request in milliseconds.
	// If request took more time than set in 'timeout', request will be aborted and
	// fail callbacks will be invoked.
	// By default timeout is disabled.
	// Optional.
	timeout: 5000,

	// Typical set of callbacks.
	// Only success callback is required. 
	done: function(response) {
		// process data
	},
	fail: function(response) {
		// handle error
	},
	always: function() {
		// this code will be executed always
	}
});

And now, the sweetest part. You can use promise-like chainable calls to provide multiple callbacks:

jsonpy({
	url: 'url',
	timeout: 5000
}).done(function(response) {
	// process data
}).done(function(response) {
	// process data again
})
.fail(function(response) {
	// handle error
}).always(function() {
	// this code will be executed always
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.