简体   繁体   English

调用多个回调后,如何调用函数

[英]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.

相关问题 萤火虫-如何检测已调用的函数或从何处调用了函数 - firebug - how can I detect what functions have been called or from where a function has been called 当另一个已执行时,我如何调用 function - How can I call a function when another have been executed 等到所有回调都被调用 - Wait until all the callbacks have been called 如何仅在页面加载完成后以及调用另一个函数之后才调用函数 - How to call a function only after page has finished loading and after another function has been called 如何知道何时成功执行了多个回调而没有返回的诺言 - how to know when several callbacks have been successfully executed with no returned promises 如何测试一个函数没有被调用? - How can I test that a function has not been called? 我如何知道 function 是否已被调用? - How can I know if a function has been called or not? 在jquery中创建元素后如何调用函数? - How can I call a function after an element has been created in jquery? 如何在加载所有元素(由ajax调用)时调用函数? - How to call a function when all elements (called by ajax) have been loaded? 将鼠标放在div上一定时间后,如何调用一个函数? - How do you have a function called after the mouse has been on a div for a certain amount of time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM