promise.js ajax,promisejs: promise.js 是 Promises 的轻量级 JavaScript 实现

Promise.js是一个轻量级的JavaScript实现,用于处理异步操作的回调。它引入了Promise对象,允许通过.then()方法附加回调,当Promise解析时调用。异步函数通过done()方法解决Promise,调用回调。此外,可以使用promise.chain()进行异步函数的链式调用。库还提供了AJAX功能,并支持全局超时配置。Promise.js已在多个浏览器上成功测试,包括IE5.5+和FF1.5+。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

promise.js

A lightweight javascript implementation of promises.

Using the Promise Object

Promises provide an alternative to callback-passing. Asynchronous functions return a Promise object onto which callbacks can be attached.

Callbacks are attached using the .then(callback) method. They will be called when the promise is resolved.

var p = asyncfoo(a, b, c);

p.then(function(error, result) {

if (error) return;

alert(result);

});

Asynchronous functions must resolve the promise with the .done() method when their task is done. This invokes the promise callback(s) with the same arguments that were passed to .done().

function asyncfoo() {

var p = new promise.Promise(); /* (1) create a Promise */

setTimeout(function() {

p.done(null, "O hai!"); /* (3) resolve it when ready */

}, 1000);

return p; /* (2) return it */

}

A Word on Callback Signatures

Although an arbitrary number of arguments are accepted for callbacks, the following signature is recommended: callback(error, result).

The error parameter can be used to pass an error code such that error != false in case something went wrong; the result parameter is used to pass a value produced by the asynchronous task. This allows callbacks to be written like this:

function callback(error, result) {

if (error) {

/* Deal with error case. */

...

return;

}

/* Deal with normal case. */

...

}

Chaining Asynchronous Functions

There are two ways of chaining asynchronous function calls. The first one is to make the callback return a promise object and to chain .then() calls. Indeed, .then() returns a Promise that is resolved when the callback resolves its promise.

Example:

function late(n) {

var p = new promise.Promise();

setTimeout(function() {

p.done(null, n);

}, n);

return p;

}

late(100).then(

function(err, n) {

return late(n + 200);

}

).then(

function(err, n) {

return late(n + 300);

}

).then(

function(err, n) {

return late(n + 400);

}

).then(

function(err, n) {

alert(n);

}

);

The other option is to use promise.chain(). The function expects an array of asynchronous functions that return a promise each. promise.chain() itself returns a Promise.

promise.chain([f1, f2, f3, ...]);

Example:

function late(n) {

var p = new promise.Promise();

setTimeout(function() {

p.done(null, n);

}, n);

return p;

}

promise.chain([

function() {

return late(100);

},

function(err, n) {

return late(n + 200);

},

function(err, n) {

return late(n + 300);

},

function(err, n) {

return late(n + 400);

}

]).then(

function(err, n) {

alert(n);

}

);

Joining Functions

promise.join([p1, p2, p3, ...]);

promise.join() expects an array of Promise object and returns a Promise that will be resolved once all the arguments have been resolved. The callback will be passed an array containing the values passed by each promise, in the same order that the promises were given.

Example:

function late(n) {

var p = new promise.Promise();

setTimeout(function() {

p.done(null, n);

}, n);

return p;

}

promise.join([

late(400),

late(800)

]).then(

function(results) {

var res0 = results[0];

var res1 = results[1];

alert(res0[1] + "" + res1[1]);

}

);

AJAX Functions Included

Because AJAX requests are the root of much asynchrony in Javascript, promise.js provides the following functions:

promise.get(url, data, headers)

promise.post(url, data, headers)

promise.put(url, data, headers)

promise.del(url, data, headers)

data (optional) : a {key: value} object or url-encoded string.

headers (optional) : a {key: value} object (e.g. {"Accept": "application/json"}).

Example:

promise.get('/').then(function(error, text, xhr) {

if (error) {

alert('Error' + xhr.status);

return;

}

alert('The page contains' + text.length + 'character(s).');

});

You can set a time in milliseconds after which unresponsive AJAX

requests should be aborted. This is a global configuration option,

disabled by default.

/* Global configuration option */

promise.ajaxTimeout = 10000;

Browser compatibility

The library has been successfully tested on IE5.5+ and FF1.5+

License

promise.js is licensed under the New BSD License.

Have fun!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值