[英]How can I call a function after several callbacks have been called
I need to push the data into a new array from the database, which will return promise object. 我需要将数据从数据库推送到新数组中,这将返回Promise对象。 5 times I need to call. 我需要打电话5次。 So I do it like this. 所以我这样做。
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); }); }); }
But if I want to use items array after all of data are pushed into it, such as call splitData(items). 但是,如果要在将所有数据推送到其中之后使用items数组,例如调用splitData(items)。 how can I that. 我怎么能 Thank you. 谢谢。
So is it something like this? 那是这样吗?
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...
})
If so, then the problem is that this " .then
chain" isn't really a chain: the "top level .then
"s will be executed one after another, but inside each of them you start a new asynchronous operation separately. 如果是这样,那么问题就在于该“ .then
链”实际上不是一个链:“顶层.then
”将一个接一个地执行,但是在每个内部,您将分别启动一个新的异步操作。 Each of these will eventually push some data to items, but the end result cannot be captured in the last " .then
". 这些中的每一个最终都会将一些数据推入项目,但是最终结果无法在最后一个“ .then
”中捕获。
If you simply attach multiple "fulfilled" callbacks to the same promise, and you don't return a promise from them, they will simply be execute one after the other, when your first promise is fulfilled: 如果您只是将多个“已完成”的回调附加到同一个promise,而没有从中返回promise,则当您的第一个promise满足时,它们将一个接一个地执行:
promise
.then(function() { foo; } // will be executed when promise is fulfilled
.then(function() { bar; } // will be executed right after
If you run other async operations in your callbacks, and want to create a chain, you need to return a promise from the callback: 如果您在回调中运行其他异步操作,并想创建一个链,则需要从回调中返回一个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
(For details, see The Promise Resolution Procedure in the spec .) (有关详细信息,请参阅规范中的Promise Resolution Procedure。)
So the original example could be reorganised like this to create a single chain: 因此,可以像这样重组原始示例以创建单个链:
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.