jQuery 当输出承诺对象而不是结果时
jQuery when output the promise object rather than result
我想获取 PouchDB 中每个数据库的计数。当我试图将它包装在 jQuery 承诺中时,当它完成时,它将承诺对象而不是结果发送到 then();
function validateAppData(event) {
var db1 = new PouchDB( "distributors" );
var db2 = new PouchDB( "categories" );
var db3 = new PouchDB( "genericTypes" );
var db4 = new PouchDB( "products" );
$.when(
db1.info(),
db2.info(),
db3.info(),
db4.info()
).then( function( distributors, categories, genericTypes, products ) {
console.log(distributors);
console.log(categories);
console.log(genericTypes);
console.log(products);
console.log("all done")
console.log(distributors.doc_count);
} );
}
控制台输出如下。
请告诉我如何在每个对象中获得响应。
谢谢。
使用Promise.all()
。 jQuery 延迟对象不一定像处理本机 Promise
方法一样处理 Promise
对象
您可以使用 Promise.all()
来处理多个 promise 并一次查看 docs。
var db1 = new PouchDB("distributors");
var db2 = new PouchDB("categories");
var db3 = new PouchDB("genericTypes");
var db4 = new PouchDB("products");
Promise.all([
db1.info(),
db2.info(),
db3.info(),
db4.info()
]).then(([ distributors, categories, genericTypes, products ]) => {
console.log(distributors);
console.log(categories);
console.log(genericTypes);
console.log(products);
console.log("all done");
console.log(distributors.doc_count);
});
我想获取 PouchDB 中每个数据库的计数。当我试图将它包装在 jQuery 承诺中时,当它完成时,它将承诺对象而不是结果发送到 then();
function validateAppData(event) {
var db1 = new PouchDB( "distributors" );
var db2 = new PouchDB( "categories" );
var db3 = new PouchDB( "genericTypes" );
var db4 = new PouchDB( "products" );
$.when(
db1.info(),
db2.info(),
db3.info(),
db4.info()
).then( function( distributors, categories, genericTypes, products ) {
console.log(distributors);
console.log(categories);
console.log(genericTypes);
console.log(products);
console.log("all done")
console.log(distributors.doc_count);
} );
}
控制台输出如下。
请告诉我如何在每个对象中获得响应。
谢谢。
使用Promise.all()
。 jQuery 延迟对象不一定像处理本机 Promise
方法一样处理 Promise
对象
您可以使用 Promise.all()
来处理多个 promise 并一次查看 docs。
var db1 = new PouchDB("distributors");
var db2 = new PouchDB("categories");
var db3 = new PouchDB("genericTypes");
var db4 = new PouchDB("products");
Promise.all([
db1.info(),
db2.info(),
db3.info(),
db4.info()
]).then(([ distributors, categories, genericTypes, products ]) => {
console.log(distributors);
console.log(categories);
console.log(genericTypes);
console.log(products);
console.log("all done");
console.log(distributors.doc_count);
});