简体   繁体   English

"如何将 Promise 加入 Promise 链"

[英]How to join promise to a promise chain

I have func1<\/code> function which returns a promise.我有func1<\/code>函数,它返回一个承诺。 In func2<\/code> i have started promise chain.func2<\/code>中,我已经启动了承诺链。 What i want to do here is, i want to use func1<\/code> resolve message in old promise chain, and i want this code to be less complex.我想在这里做的是,我想在旧的承诺链中使用func1<\/code>解析消息,并且我希望这段代码不那么复杂。 What is the best way to join func1<\/code> promise to promise chain in func2<\/code>func2<\/code>中加入func1<\/code>承诺以承诺链的最佳方式是什么

var func1 = function(){
  return new promise(function(resolve, reject){
    //some operations here
  });
};

var func2 = function(){
  promise.resolve(someFuncSync())
    .then(function(){
    //this is the old promise chain

        func1()
          .then(function(message,error){
             return message;
             //i want use this return value in old promise chain
          });

         console.log(message);
        //printing  func1 returned message in old promise chain
    })
};

Just return the new promise from within a .then() handler and it will automatically be added to the previous chain and will then control the resolved value of the old promise chain. 只需从.then()处理程序中返回新的.then() ,它将自动添加到上一个链中,然后控制旧的Promise链的解析值。

The outer promise won't resolve until the newly returned promise is resolved and the inner promise will control the final resolved value. 在新返回的承诺得到解决之前,外部承诺不会得到解决,而内部承诺将控制最终的解决值。 I added the return statement here in front of your call to return func1() to add it to the chain: 我在return func1()调用之前在此处添加了return语句,以将其添加到链中:

var func2 = function(){
  promise.resolve(someFuncSync())
    .then(function(){
    //this is the old promise chain

        // ADDED return here
        return func1()
          .then(function(message,error){
             return message;
             //i want use this return value in old promise chain
          });
    })
};

There are several other things I'd change in your code because it looks like everything you have above can be distilled down to just this: 我还要在代码中更改其他几件事,因为看起来您上面的所有内容都可以简化为:

var func2 = function () {
    someFuncSync();
    return func1();
};

This allows you do then do: 然后,您可以执行以下操作:

func2().then(function(message) {
    // process message here
}, function(err) {
    // process err here
});

Summary of changes: 变更摘要:

  1. There's no need to wrap someFuncSync() into promise if it's always synchronous. 如果始终保持同步状态,则无需将someFuncSync()包装到promise中。 You can just call it and then start your promise chain. 您可以调用它,然后启动您的诺言链。
  2. Since standard promises only return a single value (not something like (message, error) , there's really no reason for the callback with return message in it. You can just return the promise directly. 由于标准的Promise只返回一个值(而不是(message, error)类的东西,因此,实际上没有理由在其中包含return message的回调。您可以直接返回Promise。
  3. Added return in front of func1() so we are returning the promise. func1()前面添加了return ,因此我们正在返回promise。

Man, some of these answers are really overthinking things. 伙计,这些答案中有些确实太过思索了。 The beauty of promises is their simplicity: 许诺的美丽在于其简单性:

return func1().then(func2)

A .then callback may return a promise and it'll be added automatically to the chain: .then回调可能会返回一个 Promise,它会自动添加到链中:

// createURL() returns a promise that returns a URL.
createURL(...).then(url => {
  return fetch(url)
})
.then(response => {
  // :-)
})

I would do it by adding an extra step to the old promise chain. 我可以通过在旧的Promise链上增加一个额外的步骤来做到这一点。

This assumes you don't need to use the value resolved from the old promise chain in the new. 假设您不需要在新的旧承诺链中使用解析的值。

var func1 = function(){
  return new promise(function(resolve, reject){
    //some operations here
  });
};

var func2 = function(){
  promise.resolve(someFuncSync())
    .then(function(arg){

        return promise.all([
            arg, // Argument that original promise resolved with
            func1() // The new extra promise
        ])
    })
    .spread(function(arg, message){
        // Now i have the new message and also the return value from the old promise.
         console.log(message);
        //printing  func1 returned message in old promise chain
    })
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM