![](/img/trans.png)
[英]firebug - how can I detect what functions have been called or from where a function has been called
[英]How can I call a function after several callbacks have been called
我需要将数据从数据库推送到新数组中,这将返回Promise对象。 我需要打电话5次。 所以我这样做。
var items = []; function setData() { facilityAPIs.getAllListItems.get({ listName: "Samples" }). $promise.then(function(data) { alert("1"); for (var j = 0; j < data.items.length; j++) { items.push(data.items[j]); } console.log(items); }).then(function() { facilityAPIs.getAllListItems.get({ listName: "Controls" }). $promise.then(function(data) { alert("2"); for (var j = 0; j < data.items.length; j++) { items.push(data.items[j]); } console.log(items); }); }).then(function() { facilityAPIs.getAllListItems.get({ listName: "FibroBlast" }). $promise.then(function(data) { alert("3"); for (var j = 0; j < data.items.length; j++) { items.push(data.items[j]); } console.log(items); }); }).then(function() { facilityAPIs.getAllListItems.get({ listName: "pBMC" }). $promise.then(function(data) { alert("4"); for (var j = 0; j < data.items.length; j++) { items.push(data.items[j]); } console.log(items); }); }).then(function() { facilityAPIs.getAllListItems.get({ listName: "iPS Cell Lines" }). $promise.then(function(data) { alert("5"); for (var j = 0; j < data.items.length; j++) { items.push(data.items[j]); } console.log(items); }); }); }
但是,如果要在将所有数据推送到其中之后使用items数组,例如调用splitData(items)。 我怎么能 谢谢。
那是这样吗?
var items = [];
some_fn(...)
.then(function(data) { [push data to items] })
.then(function() {
some_fn(...).then(function(data) { [push data to items] });
})
.then(function() {
some_fn(...).then(function(data) { [push data to items] });
})
.then(function() {
some_fn(...).then(function(data) { [push data to items] });
})
.then(function() {
some_fn(...).then(function(data) { [push data to items] });
})
.then(function() {
...use items somehow after _all_ data has been pushed to it...
})
如果是这样,那么问题就在于该“ .then
链”实际上不是一个链:“顶层.then
”将一个接一个地执行,但是在每个内部,您将分别启动一个新的异步操作。 这些中的每一个最终都会将一些数据推入项目,但是最终结果无法在最后一个“ .then
”中捕获。
如果您只是将多个“已完成”的回调附加到同一个promise,而没有从中返回promise,则当您的第一个promise满足时,它们将一个接一个地执行:
promise
.then(function() { foo; } // will be executed when promise is fulfilled
.then(function() { bar; } // will be executed right after
如果您在回调中运行其他异步操作,并想创建一个链,则需要从回调中返回一个promise:
promise
.then(function() { return promise_foo; } // will be executed when promise is fulfilled
.then(function() { return promise_bar; } // will be executed when promise_foo is fulfilled
(有关详细信息,请参阅规范中的Promise Resolution Procedure。)
因此,可以像这样重组原始示例以创建单个链:
some_fn(...)
.then(function(data) { [push data to items] })
.then(function() {
return some_fn(...).then(function(data) { [push data to items] });
})
.then(function() {
return some_fn(...).then(function(data) { [push data to items] });
})
.then(function() {
return some_fn(...).then(function(data) { [push data to items] });
})
.then(function() {
return some_fn(...).then(function(data) { [push data to items] });
})
.then(function() {
...all operations have finished, you can use items here...
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.