简体   繁体   English

JavaScript Chaining Promises:在上一个完成之前调用下一个promise

[英]JavaScript Chaining Promises : Calling next promise before previous has finished

Tools: JavaScript ES6 工具:JavaScript ES6

I haven't seen a good succinct answer about the syntax of chaining multiple promises to execute in order. 我还没有看到关于链接多个promises以按顺序执行的语法的简洁答案。 I thought this would be a good nail in the coffin question for all promise newbies out there. 我认为这对于所有承诺新手的棺材问题都是一个很好的指甲。 :) :)

My Issue is that I want to call this in a synchronous order getPosts--->getThreads--->initializeComplete() 我的问题是我想以同步顺序调用它getPosts--->getThreads--->initializeComplete()

Here is what I am doing. 这就是我在做的事情。

userPromise.then(getPostsPromise).then(getThreadsPromise).then(initializeComplete());
  • userPromise is Promise obj I returned from another part of the code userPromise是Promise obj我从代码的另一部分返回
  • getPostsPromise returns a Promise and makes a fetch to the server for posts getPostsPromise返回一个Promise并向服务器提取帖子
  • getThreadsPromise returns a Promise and makes a fetch to the server for threads getThreadsPromise返回一个Promise并为服务器提取线程
  • initializeComplete is a callback to tell my program that it is initialized. initializeComplete是一个回调,告诉我的程序它已初始化。

Here is an example of one of the promises in the chain: 以下是链中一个承诺的示例:

var getPostsPromise = function(){
    //Firebase is just a simple server I'm using
    var firebasePostsRef = new Firebase("https://myfburl.firebaseio.com/posts");
    var postsRef = firebasePostsRef.child(localPlace.key);

    return new Promise(function(resolve, reject) {
      //Below is a Firebase listener that is called when data is returned
      postsRef.once('value', function(snap,prevChild) {
            var posts = snap.val();
            AnotherFile.receiveAllPosts(posts);
            resolve(posts);
        }); 
      });
}

But initializeComplete() is being called before getPostsPromise and getThreadsPromise have a chance to finish fetching. 但是在getPostsPromisegetThreadsPromise有机会完成提取之前调用initializeComplete()

Why is that happening and how do I write the promises to execute in order? 为什么会发生这种情况?如何编写承诺以按顺序执行?

initializeComplete is getting called right away because you are invoking it when passing it to then . initializeComplete获取调用的时候了,因为你把它传递给时调用它then You have to omit the parentheses, just like you did for getPostsPromise and getThreadsPromise 你必须省略括号,就像你为getPostsPromisegetThreadsPromise所做的那样

userPromise.then(getPostsPromise).then(getThreadsPromise).then(initializeComplete);

While yts's answer is correct (the issue is you're invoking initializeComplete instead of passing the function), I'd rather format the calls a bit differently. 虽然yts的答案是正确的(问题是你正在调用initializeComplete而不是传递函数),我宁愿格式化调用有点不同。 Having each callback function call the next function is a bit against the design of promises. 让每个回调函数调用下一个函数有点违背promises的设计。 I'd rather each function return a promise, and then call then on the returned promise: 我宁愿每个函数返回一个承诺,然后调用then在返回的承诺:

userPromise
.then(function(){
  return getPostsPromise()
}).then(function(){
  return getThreadsPromise()
}).then(function(){
  return initializeComplete();
});

or to pass the actual returned objects and not have to do any additional intermediate processing: 或者传递实际返回的对象而不必进行任何额外的中间处理:

userPromise
.then(getPostsPromise)
.then(getThreadsPromise)
.then(initializeComplete);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript的承诺:链接许诺混乱 - JavaScript Promises: Chaining promise confusion JavaScript / Promise-定义Promise链接之间的超时 - JavaScript/Promise - Define timeout between promises chaining 在下一个开始之前等待先前承诺的Javascript Map? - Javascript Map that waits for previous promise before starting next? 链接承诺,但在一个失败时继续下一个承诺(并记录拒绝) - Chaining promises but continue with the next promise when one fails (and log the rejection) 链接承诺与以前的结果 - Chaining promises with previous results 如何确保函数在调用另一个函数之前完成-Promise Chaining Angular JS? - How To Ensure A Function Has Completed Before Calling Another - Promise Chaining Angular JS? Promise.all在它所依赖的承诺之前完成 - Promise.all is finished before the promises that it's relying on 如何使用在等待下一个Prom之前等待jquery ajax完成的JavaScript Promise? - How to use javascript promises that waits for jquery ajax to finish before moving onto next promise? 上一个完成后加载下一张图像 - Load next image once previous has finished 是否可以保证承诺链确保仅在前一个承诺完成后才调用下一个承诺的回调? - Does promise-chaining guarantee that a callback of the next promise is called only after preceding one is finished?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM